:root {
    --bg-color: #0f172a;
    --text-color: #f8fafc;
    --glass-bg: rgba(30, 41, 59, 0.7);
    --glass-border: rgba(255, 255, 255, 0.1);
    --key-bg: rgba(51, 65, 85, 0.5);
    --key-hover: rgba(99, 102, 241, 0.8);
    --key-active: rgba(79, 70, 229, 1);
    --accent-color: #6366f1;
    --num-color: #38bdf8;
    --op-color: #a78bfa;
    --action-color: #fbbf24;
    --delete-color: #f43f5e;
}

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

body {
    font-family: 'Inter', sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    min-height: 100vh;
    min-height: 100dvh; /* dynamic viewport height — fixes mobile browser chrome */
    display: flex;
    justify-content: center;
    align-items: center;
    background-image:
        radial-gradient(at 0% 0%, rgba(99, 102, 241, 0.15) 0px, transparent 50%),
        radial-gradient(at 100% 100%, rgba(236, 72, 153, 0.15) 0px, transparent 50%);
}

.app-container {
    width: 100%;
    max-width: 800px;
    padding: 2rem;
    display: flex;
    flex-direction: column;
    gap: 2rem;
}

header {
    text-align: center;
}

header h1 {
    font-size: 2.5rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
    background: linear-gradient(to right, #818cf8, #c084fc);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

header p {
    color: #94a3b8;
    font-size: 1.1rem;
}

.math-input-wrapper {
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    border-radius: 1rem;
    padding: 1.5rem;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    /* Prevent overflow on small screens */
    overflow: hidden;
}

math-field {
    font-size: 2rem;
    width: 100%;
    background: transparent;
    color: var(--text-color);
    padding: 0.5rem;
    outline: none;
    --caret-color: var(--accent-color);
    --selection-background-color: rgba(99, 102, 241, 0.3);
    --selection-color: var(--text-color);
}

math-field::part(virtual-keyboard-toggle) {
    display: none; /* Hide MathLive's default toggle since we have a custom keyboard */
}

/* ─── Virtual Keyboard ─────────────────────────────────────────────── */
.virtual-keyboard {
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    border-radius: 1rem;
    padding: 1rem;
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.keyboard-tabs {
    display: flex;
    gap: 0.5rem;
    overflow-x: auto;
    padding-bottom: 0.5rem;
    -webkit-overflow-scrolling: touch; /* iOS momentum scroll */
    scrollbar-width: none;            /* Firefox: hide scrollbar */
}

.keyboard-tabs::-webkit-scrollbar {
    display: none; /* Chrome/Safari: hide scrollbar */
}

.tab-btn {
    background: transparent;
    border: none;
    color: #94a3b8;
    font-family: inherit;
    font-size: 1rem;
    font-weight: 500;
    padding: 0.5rem 1rem;
    border-radius: 0.5rem;
    cursor: pointer;
    transition: all 0.2s ease;
    white-space: nowrap;
    /* Touch: kill 300 ms delay & blue flash */
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    /* Minimum tap target */
    min-height: 44px;
    display: flex;
    align-items: center;
}

.tab-btn:hover {
    color: var(--text-color);
    background: rgba(255, 255, 255, 0.05);
}

.tab-btn.active {
    color: var(--text-color);
    background: var(--accent-color);
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4);
}

.keyboard-pane {
    display: none;
    grid-template-columns: repeat(7, 1fr);
    gap: 0.5rem;
}

.keyboard-pane.active {
    display: grid;
    animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(5px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* ─── Keys ─────────────────────────────────────────────────────────── */
.key {
    background: var(--key-bg);
    border: 1px solid var(--glass-border);
    color: var(--text-color);
    font-family: inherit;
    font-size: 1.2rem;
    padding: 1rem 0;
    border-radius: 0.5rem;
    cursor: pointer;
    transition: all 0.1s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    justify-content: center;
    align-items: center;
    user-select: none;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
    /* Touch: eliminate 300 ms delay & blue highlight */
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    /* WCAG 2.5.5 — minimum 44 × 44 px touch target */
    min-height: 44px;
}

.key:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.key:active {
    transform: translateY(1px);
    background: var(--key-active);
}

.num-key {
    color: var(--num-color);
    font-weight: 600;
    font-size: 1.4rem;
}

.op-key {
    color: var(--op-color);
}

.action-key {
    color: var(--action-color);
    font-weight: 600;
}

.delete-key {
    color: var(--delete-color);
    background: rgba(244, 63, 94, 0.1);
}

.delete-key:hover {
    background: rgba(244, 63, 94, 0.3);
}

/* ─── Tablet  601 – 768 px ─────────────────────────────────────────── */
@media (max-width: 768px) and (min-width: 601px) {
    .app-container {
        padding: 1.5rem;
        gap: 1.5rem;
    }

    header h1 { font-size: 2rem; }
    header p  { font-size: 1rem; }

    math-field { font-size: 1.7rem; }

    .keyboard-pane {
        grid-template-columns: repeat(6, 1fr);
        gap: 0.45rem;
    }

    .key {
        padding: 0.85rem 0;
        font-size: 1.1rem;
    }

    .num-key { font-size: 1.25rem; }
}

/* ─── Phone  ≤ 600 px ──────────────────────────────────────────────── */
@media (max-width: 600px) {

    body {
        align-items: flex-start;
    }

    .app-container {
        padding: 0.875rem 0.75rem;
        /* Reserve space for iPhone home bar */
        padding-bottom: calc(0.875rem + env(safe-area-inset-bottom));
        gap: 0.875rem;
        min-height: 100dvh;
    }

    header {
        /* Respect iPhone notch / status bar */
        padding-top: env(safe-area-inset-top);
    }

    header h1 {
        font-size: 1.5rem;
        margin-bottom: 0.2rem;
    }

    header p { font-size: 0.875rem; }

    .math-input-wrapper {
        padding: 0.875rem 1rem;
        border-radius: 0.75rem;
    }

    math-field {
        font-size: 1.4rem;
        padding: 0.25rem;
    }

    /* ── Keyboard stays visible while scrolling ── */
    .virtual-keyboard {
        padding: 0.75rem;
        gap: 0.55rem;
        border-radius: 0.75rem;
        position: sticky;
        bottom: 0;
        /* Push above iPhone home bar */
        margin-bottom: env(safe-area-inset-bottom);
    }

    .keyboard-tabs {
        gap: 0.3rem;
        padding-bottom: 0.3rem;
    }

    .tab-btn {
        font-size: 0.85rem;
        padding: 0.4rem 0.7rem;
        min-height: 40px;
        border-radius: 0.4rem;
    }

    /* 4 columns — comfortable finger tap */
    .keyboard-pane {
        grid-template-columns: repeat(4, 1fr);
        gap: 0.4rem;
    }

    .key {
        padding: 0;
        height: 52px;
        font-size: 1rem;
        border-radius: 0.4rem;
        box-shadow: none;
    }

    /* Lift-on-hover looks odd on touch; use scale instead */
    .key:hover {
        transform: none;
        box-shadow: none;
    }

    .key:active {
        transform: scale(0.93);
        background: var(--key-active);
    }

    .num-key    { font-size: 1.15rem; }
    .action-key { font-size: 1.1rem;  }
}

/* ─── Very small phones  ≤ 375 px ─────────────────────────────────── */
@media (max-width: 375px) {
    header h1 { font-size: 1.25rem; }
    header p  { font-size: 0.8rem;  }

    .tab-btn {
        font-size: 0.78rem;
        padding: 0.35rem 0.55rem;
    }

    .key {
        height: 46px;
        font-size: 0.875rem;
    }

    .num-key { font-size: 1rem; }
}
