html, body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    background-color: #4a4b55;
}

@media (hover: hover) and (pointer: fine) {
    html, body { overflow: hidden; }
}

/* ─── 1. CONTAINER & SCREEN ─── */
.calculator-container {
    width: 400px;
    background-color: #2b2b2b;
    padding: 20px;
    border-radius: 15px;
    box-sizing: border-box;
    font-family: sans-serif;
    transform-origin: top center;
    flex-shrink: 0;
    overflow: hidden; /* FIX: ensures border-radius clips child elements */
}

.screen-area {
    background-color: #ffffff;
    transition: background-color 0.15s ease;
    padding: 12px 15px 8px 15px;
    margin-bottom: 20px;
    border-radius: 5px;
    min-height: 196px;
    max-height: 196px;
    overflow-y: hidden;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: stretch;
}

/* ─── SCALE WRAPPER ─── */
/* FIX #3: prevent double-tap zoom and touch callouts globally */
* {
    -webkit-touch-callout: none;
    touch-action: manipulation; /* kills double-tap zoom on all elements */
}

/* FIX #2: use dvh (dynamic viewport height) so mobile browsers with
   address-bar show/hide don't cause scroll. Falls back to 100vh. */
#scale-wrapper {
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    background-color: #4a4b55;
    overflow: hidden;
}

/* ─── SCREEN LINES ─── */
#live-input {
    font-size: 1.2rem;
    font-weight: bold;
    font-family: sans-serif;
    margin: 0 0 8px 0;
    line-height: 1.2;
    color: #000000 !important;
    text-align: left;
    border-bottom: 1px solid #bbb;
    width: 100%;
    white-space: normal;
    overflow-wrap: break-word;
    overflow: hidden;
    flex-shrink: 0;
}

.screen-area.color-active #live-input {
    border-bottom-color: #ffffff;
    border-bottom-width: 1px;
}

#math-breakdown {
    width: 100%;
    font-size: 0.9rem;
    font-weight: normal;
    font-family: sans-serif;
    margin: 2px 0;
    text-align: right;
    color: #777777 !important;
    white-space: normal;
    word-wrap: break-word;
    overflow-wrap: normal;
    line-height: 1.15;
    overflow-x: hidden;
    overflow-y: auto;
    flex: 0 1 auto;    /* only as tall as content, shrink if needed */
    min-height: 0;     /* allow shrinking below natural size */
}

/* Line 3: Dice Totaled — 1 line max */
#dice-totaled {
    width: 100%;
    font-size: 0.9rem;
    font-weight: normal;
    font-family: sans-serif;
    margin: 2px 0;
    text-align: right;
    color: #0f5ba7 !important;
    white-space: normal;
    word-wrap: break-word;
    overflow-wrap: normal;
    line-height: 1.15;
    overflow-x: hidden;
    overflow-y: auto;
    max-height: 1.15em;
    flex-shrink: 0;
}

/* Line 4: Type Summary — 1 line max */
#type-summary {
    width: 100%;
    font-size: 0.9rem;
    font-weight: normal;
    font-family: sans-serif;
    margin: 2px 0;
    text-align: right;
    color: #6b7c3a !important;
    white-space: normal;
    word-wrap: break-word;
    overflow-wrap: break-word;
    line-height: 1.15;
    overflow-x: hidden;
    overflow-y: auto;
    max-height: 1.15em;
    flex-shrink: 0;
}

/* Scrollbars visible on touch devices, hidden on desktop (mouse) */
#math-breakdown::-webkit-scrollbar,
#dice-totaled::-webkit-scrollbar,
#type-summary::-webkit-scrollbar { width: 4px; }
#math-breakdown::-webkit-scrollbar-thumb,
#dice-totaled::-webkit-scrollbar-thumb,
#type-summary::-webkit-scrollbar-thumb { background: #ccc; border-radius: 4px; }

@media (hover: hover) and (pointer: fine) {
    /* Mouse/desktop — hide scrollbars entirely but keep scroll functional */
    #math-breakdown, #dice-totaled, #type-summary {
        scrollbar-width: none;         /* Firefox */
        -ms-overflow-style: none;      /* IE/Edge */
    }
    #math-breakdown::-webkit-scrollbar,
    #dice-totaled::-webkit-scrollbar,
    #type-summary::-webkit-scrollbar { width: 0; }
}

#final-result {
    width: 100%;
    font-size: 1.2rem;
    font-weight: bold;
    font-family: sans-serif;
    margin: 2px 0;
    line-height: 1.2;
    text-align: right;
    color: #000000 !important;
}

/* ─── COLOR MODE ─── */
.screen-area.color-active {
    background-color: #c0c0c0 !important;
}

/* ─── 2. GRID & BASE BUTTON ─── */
.calculator-grid {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    grid-auto-rows: 1fr;
    gap: 8px;
}

button {
    border: none;
    border-radius: 4px;
    padding: 11px 0;
    font-weight: bold;
    font-size: 16px;
    cursor: pointer;
    text-transform: uppercase;
    font-family: sans-serif;
}

/* ─── 3. COLOR PALETTE ─── */

/* Numbers */
button[data-action="0"], button[data-action="1"], button[data-action="2"],
button[data-action="3"], button[data-action="4"], button[data-action="5"],
button[data-action="6"], button[data-action="7"], button[data-action="8"],
button[data-action="9"] {
    background-color: #ffffff !important;
    color: #000000 !important;
    font-size: 22px;
    padding: 8px 0;
}

/* Dark Grey: Utility */
button[data-action="resi"],
button[data-action="vuln"],
button[data-action="avrg"],
button[data-action="adv"],
button[data-action="dis"],
button[data-action="clr"],
button[data-action="del"],
#btn-color {
    background-color: #4a4a4a !important;
    color: #ffffff;
}

/* COLOR button layout */
#btn-color {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 1px;
}

/* Rainbow text when active */
#btn-color.active span:nth-child(1) { color: #ff00c8 !important; }
#btn-color.active span:nth-child(2) { color: #44ff00 !important; }
#btn-color.active span:nth-child(3) { color: #00f2ff !important; }
#btn-color.active span:nth-child(4) { color: #ff7802 !important; }
#btn-color.active span:nth-child(5) { color: #e5ff00 !important; }

/* Light grey: Parens, Nav arrows */
button[data-action="("],
button[data-action=")"],
.nav-arrows button {
    background-color: #bbbbbb !important;
    color: #000000 !important;
}

/* Grey-blue: Dice & Operators */
button[data-action="d4"],
button[data-action="d6"],
button[data-action="d8"],
button[data-action="d10"],
button[data-action="d12"],
button[data-action="d20"],
button[data-action="d100"],
button[data-action="d"],
button[data-action="/"],
button[data-action="*"],
button[data-action="-"],
button[data-action="+"],
button[data-action="="] {
    background-color: #5d6d7e !important;
    color: #ffffff !important;
}

/* Math operator buttons — Latin Modern Math for crisp, precise symbols */
button[data-action="+"],
button[data-action="-"],
button[data-action="/"],
button[data-action="*"],
button[data-action="="] {
    font-family: "Latin Modern Math", "Georgia", serif;
    font-size: 22px;
    font-weight: bold;
    padding: 8px 0;
}

/* ─── 4. DAMAGE TYPE BUTTONS ─── */
.t-forc  { background-color: #bbbbbb !important; color: #008b8b !important; }
.t-fire  { background-color: #bbbbbb !important; color: #eb160e !important; }
.t-cold  { background-color: #bbbbbb !important; color: #0c31c5 !important; }
.t-thun  { background-color: #bbbbbb !important; color: #f17d00 !important; }
.t-light { background-color: #bbbbbb !important; color: #07f9f1 !important; }
.t-acid  { background-color: #bbbbbb !important; color: #308b32 !important; }
.t-pois  { background-color: #bbbbbb !important; color: #f0ea21 !important; }
.t-radi  { background-color: #bbbbbb !important; color: #ffffff !important; }
.t-necro { background-color: #bbbbbb !important; color: #590594 !important; }
.t-psyc  { background-color: #bbbbbb !important; color: #e20fd0 !important; }
.t-bldg  { background-color: #bbbbbb !important; color: #5d4037 !important; }
.t-pierc { background-color: #bbbbbb !important; color: #000080 !important; }
.t-slash { background-color: #bbbbbb !important; color: #800000 !important; }

/* ─── NAV ARROWS ─── */
.nav-arrows {
    grid-column: 4 / 6;
    grid-row: 1 / 3;
    display: grid;
    /* 3 cols x 4 rows sub-grid for D-pad layout */
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-rows: 1fr 1fr 1fr 1fr;
    gap: 4px;
}

/* ▲ — col 2, rows 1-2 */
.nav-up {
    grid-column: 2;
    grid-row: 1 / 3;
}

/* ◄ — col 1, rows 2-3 */
.nav-left {
    grid-column: 1;
    grid-row: 2 / 4;
}

/* ▼ — col 2, rows 3-4 */
.nav-down {
    grid-column: 2;
    grid-row: 3 / 5;
}

/* ► — col 3, rows 2-3 */
.nav-right {
    grid-column: 3;
    grid-row: 2 / 4;
}

/* ─── 5. DYNAMIC COLOR MODE SPANS ─── */
.fire  { color: #eb160e; }
.cold  { color: #0c31c5; }
.acid  { color: #308b32; }
.thun  { color: #f17d00; }
.light { color: #07f9f1; }
.pois  { color: #fff93e; }
.radi  { color: #ffffff; }
.necro { color: #590594; } /* FIX: was #000 — invisible on dark; now purple */
.forc  { color: #008b8b; }
.psyc  { color: #e20fd0; }
.bldg  { color: #5d4037; }
.pierc { color: #000080; }
.slash { color: #800000; }

/* Dice text color in input */
.dice-text { color: #2c6fad; font-weight: bold; }

/* op-pair: operator + its following token stay together, no line break between */
/* Zero-width space before each op allows break before operator, not after */
.op-pair { display: inline; }
.op      { display: inline; }

/* Hint extra line — shown only during hints, hidden otherwise */
#hint-extra {
    display: none;
    width: 100%;
    font-size: 0.9rem;
    font-weight: normal;
    font-family: sans-serif;
    margin: 2px 0;
    text-align: right;
    color: #777777;
    white-space: normal;
    word-wrap: break-word;
    overflow-wrap: normal;
    line-height: 1.15;
}

/* Divider shown only when at least one of lines 2-4 is visible */
#screen-divider {
    display: none;
    border: none;
    border-top: 1px solid #bbb;
    margin: 2px 0 2px 0;
    width: 100%;
    flex-shrink: 0;
}

.screen-area.color-active #screen-divider {
    border-top: 1px solid #ffffff;
}

/* ─── D20 HIGHLIGHTS ─── */
.crit {
    color: #308b32;
    font-weight: bold;
}
.fumble {
    color: #ff0000;
    font-weight: bold;
}

/* ─── DESKTOP: small top margin, centered ─── */
@media (hover: hover) and (pointer: fine) {
    #scale-wrapper {
        align-items: flex-start;
        padding-top: 20px;
    }
}

/* ─── CURSOR ─── */
.cursor {
    display: inline-block;
    width: 2px;
    height: 1.1em;
    background-color: #000000;
    vertical-align: text-bottom;
    margin: 0 1px;
    animation: blink 1s step-end infinite;
}

.screen-area.color-active .cursor {
    background-color: #111111;
}

@keyframes blink {
    0%, 100% { opacity: 1; }
    50%       { opacity: 0; }
}

/* ─────────────────────────────────────────────
   MOBILE: viewport-unit scaling
   Targets touch-only devices (phones/tablets)
   Desktop is completely unaffected
───────────────────────────────────────────── */
@media (hover: none) and (pointer: coarse) {

    html, body {
        height: auto;
        padding-bottom: 1px;
    }

    #scale-wrapper {
        background-color: #2b2b2b;
        align-items: flex-start;
    }

    /* Portrait lock warning */
    @media (orientation: landscape) {
        #scale-wrapper::before {
            content: "Please rotate to portrait mode";
            position: fixed;
            inset: 0;
            background: #2b2b2b;
            color: #ffffff;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 5vw;
            font-family: sans-serif;
            z-index: 9999;
        }
        .calculator-container {
            visibility: hidden;
        }
    }

    /* Container stretches to fill full screen both axes */
    .calculator-container {
        width: 100vw;
        height: calc(var(--real-vh, 1dvh) * 100);
        padding: 3vw;
        border-radius: 0;
        display: flex;
        flex-direction: column;
        box-sizing: border-box;
        transform: none !important;
    }

    /* Screen area — proportional, shrink:0 so grid fills rest */
    .screen-area {
        padding: 3vw 3.75vw 2vw 3.75vw;
        margin-bottom: 3vw;
        border-radius: 1.25vw;
        min-height: 45vw;
        max-height: 45vw;
        flex-shrink: 0;
    }

    /* Screen text — larger for readability */
    #live-input {
        font-size: clamp(1rem, 4.5vw, 1.4rem);
        margin: 0 0 2vw 0;
    }

    #math-breakdown, #dice-totaled {
        font-size: clamp(0.85rem, 3.5vw, 1rem);
        margin: 0.5vw 0;
    }

    #type-summary {
        font-size: clamp(0.85rem, 3.5vw, 1rem);
        margin: 0.5vw 0;
    }

    #final-result {
        font-size: clamp(1rem, 4.5vw, 1.4rem);
        margin: 0.5vw 0;
    }

    /* Grid fills remaining height after screen area */
    .calculator-grid {
        gap: 2vw;
        flex: 1;
        min-height: 0;
        grid-auto-rows: 1fr;
    }

    /* Override number/operator button padding so height:100% works */
    button[data-action="0"], button[data-action="1"], button[data-action="2"],
    button[data-action="3"], button[data-action="4"], button[data-action="5"],
    button[data-action="6"], button[data-action="7"], button[data-action="8"],
    button[data-action="9"] {
        padding: 0 !important;
        height: 100% !important;
    }

    button[data-action="+"],
    button[data-action="-"],
    button[data-action="/"],
    button[data-action="="],
    button[data-action="*"] {
        padding: 0 !important;
        height: 100% !important;
    }

    /* Nav cluster gap — equal font size for all arrows */
    .nav-arrows {
        gap: 1vw;
    }
    .nav-arrows button,
    .nav-up, .nav-down, .nav-left, .nav-right {
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 0;
    }

    /* Base button — fill grid cell height */
    button {
        padding: 0;
        font-size: 4vw;
        border-radius: 1vw;
        height: 100%;
        width: 100%;
    }

    /* Number buttons */
    button[data-action="0"], button[data-action="1"], button[data-action="2"],
    button[data-action="3"], button[data-action="4"], button[data-action="5"],
    button[data-action="6"], button[data-action="7"], button[data-action="8"],
    button[data-action="9"] {
        font-size: 5.5vw !important;
    }

    /* Operator buttons */
    button[data-action="+"],
    button[data-action="-"],
    button[data-action="/"],
    button[data-action="="] {
        font-size: 6vw !important;
    }

    button[data-action="+"] {
        font-size: 7vw !important;
    }

    button[data-action="*"] {
        font-size: 6vw !important;
    }

    /* Divider */
    #screen-divider {
        margin: 0.5vw 0;
    }

    /* Allow scroll gesture so browser chrome can hide on swipe */
    #scale-wrapper {
        overflow: visible;
        height: auto;
        min-height: calc(var(--real-vh, 1dvh) * 100);
    }

    /* Scrollbar width on mobile */
    #math-breakdown::-webkit-scrollbar,
    #dice-totaled::-webkit-scrollbar,
    #type-summary::-webkit-scrollbar { width: 1vw; }
}

/* Hint arrow spans — flex centers SVG with text */
.hint-arrow {
    display: inline-flex;
    align-items: center;
    height: 1em;
    vertical-align: middle;
}

/* Tighter word spacing for hint text to fit on one line */
#type-summary.hint-active {
    word-spacing: -0.05em;
}
