* { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: #1a1a2e;
    color: #eee;
    min-height: 100vh;
    /* Safe area insets for notched phones */
    padding: 16px;
    padding:
        16px
        calc(16px + env(safe-area-inset-right))
        calc(16px + env(safe-area-inset-bottom))
        calc(16px + env(safe-area-inset-left));
}

.container {
    max-width: 560px;
    margin: 0 auto;
}

h1 {
    text-align: center;
    font-size: 1.6rem;
    margin-bottom: 20px;
    color: #4a9eff;
}

.section { margin-bottom: 20px; }

label {
    display: block;
    font-size: 0.8rem;
    color: #aaa;
    margin-bottom: 8px;
    text-transform: uppercase;
    letter-spacing: 1px;
}

/* --- Inputs --- */
/* 16px minimum font-size prevents iOS auto-zoom on focus */

.target-input {
    width: 100%;
    padding: 14px;
    font-size: 1.4rem;
    text-align: center;
    border: 2px solid #333;
    border-radius: 10px;
    background: #16213e;
    color: #fff;
    outline: none;
    -webkit-appearance: none;
    appearance: none;
}
.target-input:focus { border-color: #e94560; }

.also-input {
    width: 100%;
    padding: 12px;
    font-size: 1rem;
    border: 2px solid #333;
    border-radius: 10px;
    background: #16213e;
    color: #fff;
    outline: none;
    margin-top: 10px;
    -webkit-appearance: none;
    appearance: none;
}
.also-input:focus { border-color: #0f3460; }
.also-input::placeholder { color: #555; }

/* --- Number picker grids --- */

.small-grid {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 8px;
}

.large-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    margin-top: 8px;
}

.num-btn {
    /* Minimum 44px touch target per Apple/Google guidelines */
    min-height: 48px;
    padding: 10px 4px;
    font-size: 1.1rem;
    border: 2px solid #333;
    border-radius: 10px;
    background: #16213e;
    color: #fff;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: background 0.12s, transform 0.1s;
    user-select: none;
    touch-action: manipulation; /* Prevents 300ms delay on mobile */
}
.num-btn:hover { background: #1a3a5c; border-color: #0f3460; }
.num-btn:active { transform: scale(0.93); }
.num-btn.large-num { background: #1a2744; }
.num-btn:disabled { opacity: 0.35; cursor: not-allowed; transform: none; }

/* --- Selected number chips --- */

.selected-area {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 10px;
    min-height: 48px;
    padding: 8px;
    border: 2px dashed #333;
    border-radius: 10px;
    align-items: center;
}

.chip {
    display: flex;
    align-items: center;
    gap: 6px;
    /* 44px minimum touch target */
    min-height: 40px;
    padding: 6px 14px;
    background: #0f3460;
    border-radius: 20px;
    font-size: 1.05rem;
    cursor: pointer;
    transition: background 0.12s;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}
.chip:hover { background: #e94560; }
.chip .x { font-size: 0.8rem; opacity: 0.7; }

.placeholder-text { color: #555; font-size: 0.9rem; }

/* --- Action buttons --- */

.btn-row { display: flex; gap: 10px; }

.solve-btn, .clear-btn {
    min-height: 48px;
    padding: 14px;
    font-size: 1.1rem;
    font-weight: 600;
    border: none;
    border-radius: 10px;
    cursor: pointer;
    transition: background 0.15s;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.solve-btn {
    flex: 1;
    background: #2ecc71;
    color: #fff;
}
.solve-btn:hover { background: #27ae60; }
.solve-btn:disabled { background: #444; cursor: not-allowed; }

.clear-btn {
    background: #333;
    color: #aaa;
    padding-left: 20px;
    padding-right: 20px;
}
.clear-btn:hover { background: #444; }

.error-msg {
    color: #e94560;
    font-size: 0.9rem;
    margin-top: 8px;
    min-height: 1.2em;
}

/* --- Results --- */

.results { margin-top: 20px; }

.results-header {
    font-size: 0.9rem;
    color: #aaa;
    margin-bottom: 10px;
}

.results-list {
    max-height: 60vh;
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: #333 transparent;
    -webkit-overflow-scrolling: touch;
}

.expr-item {
    padding: 10px 12px;
    margin-bottom: 4px;
    background: #16213e;
    border-radius: 8px;
    font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
    font-size: 0.95rem;
    /* Allow long expressions to scroll horizontally rather than break */
    overflow-x: auto;
    white-space: nowrap;
}

.no-results {
    text-align: center;
    color: #666;
    padding: 24px;
    font-size: 1rem;
}

.loading {
    text-align: center;
    color: #e94560;
    padding: 24px;
    font-size: 1rem;
}

/* --- Small mobile (under 380px, e.g. iPhone SE) --- */

@media (max-width: 380px) {
    body { padding: 12px; }
    h1 { font-size: 1.35rem; }
    .num-btn { font-size: 1rem; min-height: 44px; }
    .small-grid, .large-grid { gap: 6px; }
    .chip { padding: 6px 10px; font-size: 0.95rem; }
    .expr-item { font-size: 0.85rem; padding: 8px 10px; }
}

/* --- Desktop (wider than 768px) --- */

@media (min-width: 768px) {
    body { padding: 32px; }

    .container { max-width: 600px; }

    h1 { font-size: 1.8rem; margin-bottom: 28px; }

    .section { margin-bottom: 24px; }

    .num-btn {
        min-height: 52px;
        font-size: 1.15rem;
        border-radius: 10px;
    }

    .small-grid, .large-grid { gap: 10px; }

    .target-input {
        padding: 16px;
        font-size: 1.6rem;
        border-radius: 12px;
    }

    .also-input {
        padding: 14px;
        font-size: 1.05rem;
    }

    .solve-btn, .clear-btn {
        min-height: 52px;
        font-size: 1.2rem;
    }

    .expr-item {
        font-size: 1rem;
        padding: 12px 16px;
    }

    .results-list { max-height: 70vh; }
}
