/* ==========================================================================
   360 Universal Calculator — core layout, header, navigation, settings
   Theme colour tokens are injected at runtime by js/app.js.
   ========================================================================== */

:root {
    --bg: #eef1f7;
    --surface: #ffffff;
    --text: #1e293b;
    --muted: #5b6b80;
    --border: #e2e8f0;
    --accent: #4f46e5;
    --accent-text: #ffffff;
    --key-bg: #f4f6fb;
    --key-text: #1e293b;
    --op-bg: #4f46e5;
    --op-text: #ffffff;
    --danger-bg: #fee2e2;
    --danger-text: #dc2626;

    --radius-sm: 10px;
    --radius-md: 16px;
    --radius-lg: 24px;
    --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
    --shadow-md: 0 10px 30px -14px rgba(15, 23, 42, 0.28);
    --shadow-lg: 0 24px 60px -24px rgba(15, 23, 42, 0.45);
    --gap: 12px;
    --pad: clamp(12px, 3vw, 22px);
    --font: "Cairo", "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
    --nav-height: 60px;
    --topbar-height: 58px;
    --focus: 0 0 0 3px color-mix(in srgb, var(--accent) 45%, transparent);
}

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

/* Hidden views must not take part in layout. */
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
    transition: background-color .3s ease, color .3s ease;
    overscroll-behavior: none;
}

button, input, select, textarea { font: inherit; color: inherit; }

button { cursor: pointer; border: 0; background: none; }

:focus-visible { outline: none; box-shadow: var(--focus); border-radius: var(--radius-sm); }

/* --------------------------------------------------------------------------
   Boot loader / skip link / banners
   -------------------------------------------------------------------------- */

.boot-loader {
    position: fixed;
    inset: 0;
    z-index: 60;
    display: flex;
    flex-direction: column;
    gap: 14px;
    align-items: center;
    justify-content: center;
    background: var(--bg);
    color: var(--muted);
    font-weight: 600;
}

.boot-loader__spinner {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    border: 3px solid color-mix(in srgb, var(--accent) 25%, transparent);
    border-top-color: var(--accent);
    animation: spin .8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.skip-link {
    position: absolute;
    left: -9999px;
    top: 0;
    z-index: 80;
    background: var(--accent);
    color: var(--accent-text);
    padding: 10px 16px;
    border-radius: 0 0 var(--radius-sm) 0;
}

.skip-link:focus { left: 0; }

.update-notice,
.offline-banner,
.install-banner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
    padding: 10px var(--pad);
    font-size: .9rem;
    font-weight: 600;
}

.update-notice { background: color-mix(in srgb, var(--accent) 15%, var(--surface)); }
.offline-banner { background: color-mix(in srgb, var(--danger-text) 15%, var(--surface)); color: var(--danger-text); }
.install-banner {
    background: var(--surface);
    border-top: 1px solid var(--border);
    box-shadow: var(--shadow-md);
    position: relative;
    z-index: 20;
}
.banner__body { display: flex; flex-direction: column; gap: 2px; }
.banner__body span { font-weight: 400; color: var(--muted); font-size: .85rem; }
.banner__actions { display: flex; gap: 8px; }

/* --------------------------------------------------------------------------
   App frame
   -------------------------------------------------------------------------- */

.app {
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    padding-bottom: calc(var(--nav-height) + env(safe-area-inset-bottom, 0px));
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 30;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    flex-wrap: wrap;
    min-height: var(--topbar-height);
    padding: 8px var(--pad);
    background: color-mix(in srgb, var(--surface) 88%, transparent);
    backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border);
}

.topbar__brand { display: flex; align-items: center; gap: 10px; min-width: 0; }

.topbar__logo {
    display: grid;
    place-items: center;
    width: 38px;
    height: 38px;
    border-radius: 12px;
    background: var(--accent);
    color: var(--accent-text);
    font-weight: 800;
    font-size: .95rem;
    letter-spacing: -.5px;
    flex: none;
}

.topbar__titles { display: flex; flex-direction: column; min-width: 0; }
.topbar__title { font-size: .95rem; font-weight: 800; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar__tagline { color: var(--muted); font-size: .72rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.topbar__actions { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }

.pill {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 7px 12px;
    border-radius: 999px;
    background: var(--key-bg);
    color: var(--key-text);
    font-size: .78rem;
    font-weight: 700;
    border: 1px solid transparent;
    transition: transform .12s ease, background-color .2s ease;
}

.pill:hover { border-color: color-mix(in srgb, var(--accent) 35%, transparent); }
.pill:active { transform: scale(.96); }
.pill--icon { padding: 7px 10px; font-size: .95rem; }

/* The viewport is the scroll container, so the calculator can size itself
   against a definite height. */
.viewport {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    overscroll-behavior: contain;
    display: flex;
    flex-direction: column;
}

.view {
    flex: 0 0 auto;
    display: flex;
    flex-direction: column;
    gap: var(--gap);
    padding: var(--pad);
}

.view__head { display: flex; flex-direction: column; gap: 2px; }
.view__head--row { flex-direction: row; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.view__title { font-size: 1.25rem; font-weight: 800; }
.view__subtitle { color: var(--muted); font-size: .85rem; }

/* --------------------------------------------------------------------------
   Buttons, cards, fields
   -------------------------------------------------------------------------- */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 11px 18px;
    border-radius: var(--radius-sm);
    font-weight: 700;
    font-size: .9rem;
    background: var(--key-bg);
    color: var(--key-text);
    border: 1px solid transparent;
    transition: transform .12s ease, filter .2s ease;
}

.btn:active { transform: scale(.97); }
.btn--primary { background: var(--accent); color: var(--accent-text); }
.btn--primary:hover { filter: brightness(1.06); }
.btn--ghost { background: transparent; border-color: var(--border); }
.btn--ghost:hover { background: var(--key-bg); }
.btn--danger { background: var(--danger-bg); color: var(--danger-text); }
.btn--icon { padding: 8px 14px; font-size: 1.1rem; }

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 16px;
    box-shadow: var(--shadow-sm);
}

.card__title { font-size: .95rem; font-weight: 800; margin-bottom: 12px; }

.field { display: flex; flex-direction: column; gap: 6px; font-size: .82rem; font-weight: 600; }
.field__label { color: var(--muted); }
.field__hint { color: var(--muted); font-weight: 400; font-size: .75rem; }
.field--check { flex-direction: row; align-items: center; gap: 8px; font-weight: 500; }

.input {
    width: 100%;
    padding: 11px 12px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--key-bg);
    color: var(--key-text);
    font-weight: 600;
    transition: border-color .2s ease;
}

.input:focus { border-color: var(--accent); }
.input--small { width: auto; min-width: 84px; padding: 7px 10px; }

.field-row,
.about-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 0;
    border-bottom: 1px solid var(--border);
    font-size: .88rem;
    font-weight: 600;
}

.about-row:last-of-type { border-bottom: 0; }

.toggle-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 11px 0;
    border-bottom: 1px solid var(--border);
    font-size: .88rem;
    font-weight: 600;
    cursor: pointer;
}

.toggle {
    appearance: none;
    width: 46px;
    height: 26px;
    border-radius: 999px;
    background: var(--border);
    position: relative;
    transition: background-color .2s ease;
    flex: none;
}

.toggle::after {
    content: "";
    position: absolute;
    top: 3px;
    left: 3px;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: #fff;
    box-shadow: var(--shadow-sm);
    transition: transform .2s ease;
}

.toggle:checked { background: var(--accent); }
.toggle:checked::after { transform: translateX(20px); }
[dir="rtl"] .toggle:checked::after { transform: translateX(-20px); }

.chip-row { display: flex; flex-wrap: wrap; gap: 8px; }
.chip-row--tight { gap: 6px; }

.chip {
    padding: 8px 14px;
    border-radius: 999px;
    background: var(--key-bg);
    color: var(--key-text);
    font-size: .82rem;
    font-weight: 700;
    border: 1px solid transparent;
}

.chip:hover { border-color: color-mix(in srgb, var(--accent) 40%, transparent); }
.chip.is-active { background: var(--accent); color: var(--accent-text); }

/* --------------------------------------------------------------------------
   Settings sections
   -------------------------------------------------------------------------- */

.settings-section + .settings-section { margin-top: var(--gap); }

.swatch-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
    gap: 8px;
}

.swatch {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    padding: 10px 6px;
    border-radius: var(--radius-sm);
    border: 2px solid var(--border);
    background: var(--key-bg);
    font-size: .72rem;
    font-weight: 700;
    color: var(--key-text);
}

.swatch.is-active { border-color: var(--accent); }
.swatch__dot { width: 30px; height: 30px; border-radius: 50%; border: 2px solid var(--border); }

.color-panel { margin-top: 12px; display: flex; flex-direction: column; gap: 12px; }
.color-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }

.color-field {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: .78rem;
    font-weight: 600;
    padding: 6px 8px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--key-bg);
}

.color-field input[type="color"] {
    width: 30px;
    height: 26px;
    border: 0;
    padding: 0;
    background: none;
    cursor: pointer;
}

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

.size-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    padding: 12px 8px;
    border-radius: var(--radius-md);
    border: 2px solid var(--border);
    background: var(--key-bg);
    color: var(--key-text);
    text-align: center;
}

.size-card.is-active { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, var(--key-bg)); }
.size-card__preview { display: block; border-radius: 4px; background: var(--accent); opacity: .8; }
.size-card__preview--xl { width: 46px; height: 26px; }
.size-card__preview--md { width: 34px; height: 24px; }
.size-card__preview--sm { width: 24px; height: 20px; }
.size-card__title { font-size: .8rem; font-weight: 800; }
.size-card__hint { font-size: .68rem; color: var(--muted); font-weight: 500; }

/* --------------------------------------------------------------------------
   History
   -------------------------------------------------------------------------- */

.history-list { display: flex; flex-direction: column; gap: 6px; }

.history-day {
    font-size: .74rem;
    text-transform: uppercase;
    letter-spacing: .08em;
    color: var(--muted);
    margin: 10px 0 2px;
}

.history-item {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 12px;
    width: 100%;
    padding: 12px 14px;
    border-radius: var(--radius-sm);
    background: var(--surface);
    border: 1px solid var(--border);
    text-align: start;
}

.history-item:hover { border-color: var(--accent); }
.history-expression { color: var(--muted); font-size: .82rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history-result { font-weight: 800; font-size: 1rem; white-space: nowrap; }

.empty-state { color: var(--muted); font-size: .88rem; text-align: center; padding: 24px 0; }

/* --------------------------------------------------------------------------
   Advertising slots
   -------------------------------------------------------------------------- */

.ad-slot {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    min-height: 74px;
    padding: 12px;
    border: 1px dashed var(--border);
    border-radius: var(--radius-md);
    background: color-mix(in srgb, var(--surface) 70%, transparent);
    text-align: center;
    overflow: hidden;
}

.ad-slot[hidden] { display: none; }
.ad-slot--leaderboard { min-height: 84px; }
.ad-slot__label {
    position: absolute;
    top: 4px;
    inset-inline-start: 8px;
    font-size: .6rem;
    letter-spacing: .12em;
    text-transform: uppercase;
    color: var(--muted);
}
.ad-slot__text { font-weight: 700; font-size: .85rem; color: var(--muted); }
.ad-slot__note { font-size: .68rem; color: var(--muted); opacity: .75; }
.ad-slot__close {
    position: absolute;
    top: 2px;
    inset-inline-end: 6px;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    color: var(--muted);
    font-size: 1rem;
    line-height: 1;
}
.ad-slot__close:hover { background: var(--key-bg); }

/* --------------------------------------------------------------------------
   Bottom navigation, footer, toast
   -------------------------------------------------------------------------- */

.bottom-nav {
    position: fixed;
    inset-inline: 0;
    bottom: 0;
    z-index: 40;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 2px;
    padding: 6px 6px calc(6px + env(safe-area-inset-bottom, 0px));
    background: color-mix(in srgb, var(--surface) 94%, transparent);
    backdrop-filter: blur(14px);
    border-top: 1px solid var(--border);
}

.nav {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    padding: 6px 4px;
    border-radius: var(--radius-sm);
    color: var(--muted);
    font-size: .68rem;
    font-weight: 700;
}

.nav__icon { font-size: 1.15rem; line-height: 1; }
.nav.is-active { color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, transparent); }

.footer {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    flex-wrap: wrap;
    padding: 12px var(--pad) 6px;
    color: var(--muted);
    font-size: .72rem;
}

.toast {
    position: fixed;
    inset-inline: 0;
    bottom: calc(var(--nav-height) + 16px + env(safe-area-inset-bottom, 0px));
    z-index: 50;
    margin: 0 auto;
    width: fit-content;
    max-width: min(90vw, 420px);
    padding: 10px 18px;
    border-radius: 999px;
    background: var(--text);
    color: var(--bg);
    font-size: .85rem;
    font-weight: 700;
    box-shadow: var(--shadow-lg);
    opacity: 0;
    transform: translateY(12px);
    pointer-events: none;
    transition: opacity .2s ease, transform .2s ease;
}

.toast.is-visible { opacity: 1; transform: translateY(0); }

/* --------------------------------------------------------------------------
   Full-screen mode and reduced motion
   -------------------------------------------------------------------------- */

html.is-fullscreen .topbar,
html.is-fullscreen .bottom-nav,
html.is-fullscreen .footer,
html.is-fullscreen .ad-slot,
html.is-fullscreen .install-banner,
html.is-fullscreen .offline-banner,
html.is-fullscreen .update-notice { display: none !important; }

html.is-fullscreen .app { padding-bottom: 0; }

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: .001ms !important;
        transition-duration: .001ms !important;
    }
}

/* The boot loader disappears as soon as the app is ready. */
html.is-ready .boot-loader { display: none; }

@media (max-width: 420px) {
    .topbar { gap: 6px; }
    .topbar__tagline { display: none; }
}

@media (min-width: 900px) {
    .view { max-width: 1080px; width: 100%; margin: 0 auto; }
    .bottom-nav { position: static; grid-template-columns: repeat(4, minmax(0, 140px)); justify-content: center; border-radius: var(--radius-md); margin: 0 auto 8px; border: 1px solid var(--border); }
    .app { padding-bottom: 12px; }
    .toast { bottom: 24px; }
}
