/* ==========================================================================
   CSIP12 UI — "Digital Textbook" design system (Phase 3A)
   Design tokens, shared site chrome (top bar, dropdowns, drawer, bottom nav)
   and reusable components. Every class is prefixed `csip-` so nothing here
   collides with Tailwind utilities or the existing style.css.

   Colour roles (fixed by the approved Phase 2 mockups):
     teal  = selected / active / primary action
     amber = accent (eyebrows, XP, streak) and warnings
     navy  = text and structure
   ========================================================================== */

:root {
    --c-paper: #FAF6EE;         /* page background */
    --c-band: #F1E9D8;          /* alternate band / subtle fill */
    --c-surface: #FFFFFF;       /* cards, menus, bars */
    --c-ink: #1B2A41;           /* primary text, structure */
    --c-ink-2: #4B5768;         /* secondary text */
    --c-ink-3: #8B93A0;         /* muted text, idle icons */
    --c-rule: #E4D8BC;          /* hairlines */
    --c-rule-2: #D9CBB0;        /* stronger borders */

    --c-teal: #2F6F62;          /* primary / selected */
    --c-teal-hover: #265B50;
    --c-teal-tint: #E3EFEA;
    --c-teal-text: #1E4F44;     /* text on teal tint */
    --c-on-teal: #FFFFFF;

    --c-amber: #C97A1B;         /* accent */
    --c-amber-text: #9C5E12;
    --c-amber-tint: #FBEBD3;

    --c-rust: #9A3412;          /* wrong / warning */
    --c-rust-tint: #FBEAE3;

    --c-disabled-bg: #EDE6D6;
    --c-disabled-ink: #A59C8A;
    --c-backdrop: rgba(27, 42, 65, 0.45);
    --c-bar-bg: rgba(250, 246, 238, 0.94);

    --f-serif: 'Lora', Georgia, 'Times New Roman', serif;
    --f-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;

    --r-sm: 4px;
    --r-md: 8px;
    --r-lg: 12px;
    --shadow-pop: 0 12px 32px -8px rgba(27, 42, 65, 0.22);

    --bar-h: 4rem;              /* top bar; matches the pt-16 already on every <main> */
    --bottomnav-h: 64px;        /* fixed mobile bottom UI: bottom nav, chapter action bar */
    --bottom-clear-gap: 32px;   /* extra room between the last content and that bar */
}

html.dark {
    --c-paper: #0F1726;
    --c-band: #172134;
    --c-surface: #131D2E;
    --c-ink: #ECE6D8;
    --c-ink-2: #B7BDC7;
    --c-ink-3: #8A94A3;
    --c-rule: #263247;
    --c-rule-2: #34425A;

    --c-teal: #2F7F70;
    --c-teal-hover: #37907F;
    --c-teal-tint: rgba(79, 170, 150, 0.16);
    --c-teal-text: #9BD5C7;
    --c-on-teal: #FFFFFF;

    --c-amber: #E0A04A;
    --c-amber-text: #E8B56E;
    --c-amber-tint: rgba(224, 160, 74, 0.14);

    --c-rust: #E3825D;
    --c-rust-tint: rgba(227, 130, 93, 0.14);

    --c-disabled-bg: #1C2739;
    --c-disabled-ink: #5E6A7D;
    --c-backdrop: rgba(3, 7, 18, 0.6);
    --c-bar-bg: rgba(15, 23, 38, 0.94);
    --shadow-pop: 0 14px 34px -8px rgba(0, 0, 0, 0.6);
}

/* ---------- Typography helpers ---------- */
.csip-serif { font-family: var(--f-serif) !important; }
.csip-eyebrow {
    font-family: var(--f-sans);
    font-size: 11px; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase;
    color: var(--c-amber-text);
}

/* ==========================================================================
   SITE CHROME
   ========================================================================== */

/* Body layout: the app shell now stacks top bar above the scrolling <main>. */
body.csip-shell { background: var(--c-paper); color: var(--c-ink); }

/* App-shell height. Pages scroll inside a full-height shell (.h-screen on <body>, and on the paper viewer's <main>).
   On phones 100vh is the height with the browser toolbar hidden, so while the toolbar is showing the bottom of the shell
   sat below the screen and the last content ended up under the fixed bottom bar. 100dvh is the height actually visible;
   browsers without dvh keep 100vh. Desktop is unaffected (there dvh equals vh). */
:root .h-screen { height: 100vh; height: 100dvh; }

/* ---------- Top bar ---------- */
.csip-topbar {
    position: fixed; top: 0; left: 0; right: 0; z-index: 50;
    height: var(--bar-h);
    display: flex; align-items: center; gap: 12px;
    padding: 0 12px 0 16px;
    background: var(--c-bar-bg);
    -webkit-backdrop-filter: saturate(140%) blur(8px);
    backdrop-filter: saturate(140%) blur(8px);
    border-bottom: 1px solid var(--c-rule);
    font-family: var(--f-sans);
}
@media (min-width: 768px) {
    .csip-topbar { position: relative; flex-shrink: 0; padding: 0 24px; gap: 20px; }
}
@media (min-width: 1280px) {
    .csip-topbar { padding: 0 40px; }
}

.csip-brand { display: flex; align-items: center; gap: 10px; color: var(--c-ink); text-decoration: none; flex-shrink: 0; }
.csip-brand:hover { opacity: 0.88; }
.csip-brand__mark {
    width: 32px; height: 32px; border-radius: 8px; background: #1B2A41;
    display: flex; align-items: center; justify-content: center;
    font-family: var(--f-serif); font-weight: 700; font-size: 13px; color: #C97A1B;
}
html.dark .csip-brand__mark { box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08); }
.csip-brand__name { font-family: var(--f-serif); font-weight: 700; font-size: 17px; line-height: 1; }
.csip-brand__tag { display: none; font-size: 9px; font-weight: 600; letter-spacing: 0.04em; color: var(--c-ink-3); margin-top: 3px; text-transform: uppercase; }
@media (min-width: 1024px) { .csip-brand__tag { display: block; } }

/* ---------- Desktop navigation (dropdown groups) ---------- */
.csip-nav { display: none; }
@media (min-width: 768px) {
    .csip-nav { display: flex; align-items: center; justify-content: center; gap: 4px; flex: 1; min-width: 0; }
}
@media (min-width: 1024px) { .csip-nav { gap: 14px; } }

.csip-nav__item { position: relative; }
.csip-nav__trigger {
    display: inline-flex; align-items: center; gap: 6px;
    height: 40px; padding: 0 10px;
    background: none; border: 0; border-radius: var(--r-md); cursor: pointer;
    font: 600 13.5px/1 var(--f-sans); color: var(--c-ink-2);
    box-shadow: inset 0 -2px 0 transparent;
    transition: color .15s, background-color .15s;
}
.csip-nav__trigger:hover { color: var(--c-ink); background: var(--c-band); }
.csip-nav__trigger .fa-chevron-down { font-size: 9px; transition: transform .15s; }
.csip-nav__item.is-open .csip-nav__trigger .fa-chevron-down { transform: rotate(180deg); }
.csip-nav__trigger.is-active { color: var(--c-teal); font-weight: 700; box-shadow: inset 0 -2px 0 var(--c-teal); border-radius: 0; }
html.dark .csip-nav__trigger.is-active { color: var(--c-teal-text); box-shadow: inset 0 -2px 0 var(--c-teal-text); }

.csip-menu {
    display: none;
    position: absolute; top: 100%; left: 50%; transform: translateX(-50%);
    padding-top: 8px; z-index: 60; min-width: 280px;
}
.csip-nav__item.is-open > .csip-menu { display: block; }
@media (hover: hover) and (pointer: fine) {
    .csip-nav__item:hover > .csip-menu { display: block; }
}
.csip-menu__panel {
    background: var(--c-surface); border: 1px solid var(--c-rule); border-radius: var(--r-lg);
    box-shadow: var(--shadow-pop); padding: 6px;
}
.csip-menu__label {
    padding: 8px 10px 4px; font-size: 10px; font-weight: 700; letter-spacing: 0.08em;
    text-transform: uppercase; color: var(--c-ink-3);
}
.csip-menu__link {
    display: flex; align-items: flex-start; gap: 12px; padding: 9px 10px; border-radius: var(--r-md);
    color: var(--c-ink); text-decoration: none;
}
.csip-menu__link:hover, .csip-menu__link:focus-visible { background: var(--c-band); outline: none; }
.csip-menu__link.is-current { background: var(--c-teal-tint); }
.csip-menu__link.is-current .csip-menu__title { color: var(--c-teal-text); }
.csip-menu__icon { width: 18px; flex-shrink: 0; text-align: center; margin-top: 2px; font-size: 14px; color: var(--c-ink-3); }
.csip-menu__link.is-current .csip-menu__icon { color: var(--c-teal); }
html.dark .csip-menu__link.is-current .csip-menu__icon { color: var(--c-teal-text); }
.csip-menu__title { display: block; font-size: 13.5px; font-weight: 600; line-height: 1.3; }
.csip-menu__desc { display: block; font-size: 11.5px; color: var(--c-ink-3); line-height: 1.4; margin-top: 1px; }
.csip-menu__sep { height: 1px; background: var(--c-rule); margin: 6px 8px; }

/* ---------- Top bar actions ---------- */
.csip-actions { display: flex; align-items: center; gap: 6px; margin-left: auto; flex-shrink: 0; }
@media (min-width: 768px) { .csip-actions { margin-left: 0; gap: 10px; } }

.csip-iconbtn {
    width: 38px; height: 38px; border-radius: 999px; border: 0; background: none; cursor: pointer;
    display: inline-flex; align-items: center; justify-content: center;
    color: var(--c-ink-2); font-size: 15px; text-decoration: none;
    transition: background-color .15s, color .15s;
}
.csip-iconbtn:hover { background: var(--c-band); color: var(--c-ink); }
.csip-iconbtn--danger:hover { color: var(--c-rust); background: var(--c-rust-tint); }

.csip-profile {
    display: inline-flex; align-items: center; gap: 8px; padding: 3px; border-radius: 999px;
    color: var(--c-ink); text-decoration: none; max-width: 190px;
}
.csip-profile:hover { background: var(--c-band); }
.csip-profile img { width: 32px; height: 32px; border-radius: 999px; object-fit: cover; border: 2px solid var(--c-rule); flex-shrink: 0; }
.csip-profile__name { display: none; font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding-right: 8px; }
@media (min-width: 1024px) { .csip-profile__name { display: block; } }

.csip-only-mobile { display: inline-flex; }
.csip-only-desktop { display: none !important; }
.csip-only-wide { display: none !important; }
@media (min-width: 768px) {
    .csip-only-mobile { display: none !important; }
    .csip-only-desktop { display: inline-flex !important; }
}
@media (min-width: 1024px) { .csip-only-wide { display: inline-flex !important; } }

/* ---------- Fixed site footer (desktop/tablet) ---------- */
.csip-footer { background: var(--c-bar-bg); border-color: var(--c-rule) !important; }

/* ---------- Mobile drawer (hamburger menu) ---------- */
.csip-backdrop {
    position: fixed; inset: 0; z-index: 54; background: var(--c-backdrop);
    opacity: 0; pointer-events: none; transition: opacity .2s;
}
.csip-backdrop.is-open { opacity: 1; pointer-events: auto; }
.csip-drawer {
    position: fixed; top: 0; right: 0; bottom: 0; z-index: 55;
    width: min(86vw, 330px);
    background: var(--c-surface); border-left: 1px solid var(--c-rule);
    transform: translateX(100%); transition: transform .25s ease; visibility: hidden;
    display: flex; flex-direction: column; font-family: var(--f-sans);
}
.csip-drawer.is-open { transform: none; visibility: visible; }
.csip-drawer__head {
    height: var(--bar-h); flex-shrink: 0; display: flex; align-items: center; justify-content: space-between;
    padding: 0 10px 0 16px; border-bottom: 1px solid var(--c-rule);
}
.csip-drawer__body { flex: 1; overflow-y: auto; padding: 8px 10px 24px; overscroll-behavior: contain; }
.csip-drawer__account { margin: 8px 6px 4px; padding: 14px; border: 1px solid var(--c-rule); border-radius: var(--r-lg); background: var(--c-paper); }
.csip-drawer__account p { margin: 0 0 10px; font-size: 12.5px; color: var(--c-ink-2); line-height: 1.5; }
.csip-drawer__section { margin-top: 10px; }
.csip-drawer__legal { display: flex; flex-wrap: wrap; gap: 6px 14px; padding: 16px 12px 0; margin-top: 12px; border-top: 1px solid var(--c-rule); font-size: 12px; }
.csip-drawer__legal a { color: var(--c-ink-2); text-decoration: none; }
.csip-drawer__legal a:hover { color: var(--c-teal); }
.csip-drawer__credit { padding: 10px 12px 0; font-size: 11px; color: var(--c-ink-3); }
@media (min-width: 768px) { .csip-drawer, .csip-backdrop { display: none; } }

/* ---------- Mobile bottom navigation ---------- */
.csip-bottomnav { display: none; }
@media (max-width: 767.98px) {
    .csip-bottomnav {
        position: fixed; left: 0; right: 0; bottom: 0; z-index: 45;
        display: flex; height: calc(var(--bottomnav-h) + env(safe-area-inset-bottom));
        padding-bottom: env(safe-area-inset-bottom);
        background: var(--c-surface); border-top: 1px solid var(--c-rule);
        font-family: var(--f-sans);
    }
}
.csip-bottomnav__item {
    position: relative; flex: 1; min-width: 0;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
    color: var(--c-ink-3); text-decoration: none; font-size: 10px; font-weight: 600;
    -webkit-tap-highlight-color: transparent;
}
.csip-bottomnav__item i { font-size: 17px; }
.csip-bottomnav__item.is-active { color: var(--c-teal); font-weight: 700; }
html.dark .csip-bottomnav__item.is-active { color: var(--c-teal-text); }
.csip-bottomnav__item.is-active::before {
    content: ''; position: absolute; top: 0; left: 50%; transform: translateX(-50%);
    width: 28px; height: 3px; border-radius: 0 0 3px 3px; background: currentColor;
}

/* ==========================================================================
   REUSABLE COMPONENTS
   ========================================================================== */

/* ---------- Buttons ---------- */
.csip-btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    height: 40px; padding: 0 18px; border-radius: var(--r-md); border: 1px solid transparent;
    font: 700 13.5px/1 var(--f-sans); text-decoration: none; white-space: nowrap; cursor: pointer;
    transition: background-color .15s, border-color .15s, color .15s, box-shadow .15s;
}
.csip-btn:focus-visible { outline: 2px solid var(--c-teal); outline-offset: 2px; }
.csip-btn--primary { background: var(--c-teal); color: var(--c-on-teal); }
.csip-btn--primary:hover { background: var(--c-teal-hover); color: var(--c-on-teal); }
.csip-btn--secondary { background: transparent; color: var(--c-ink); border-color: var(--c-ink); }
html.dark .csip-btn--secondary { border-color: var(--c-rule-2); }
.csip-btn--secondary:hover { background: var(--c-band); }
.csip-btn--ghost { background: transparent; color: var(--c-ink); }
.csip-btn--ghost:hover { background: var(--c-band); }
.csip-btn--sm { height: 34px; padding: 0 14px; font-size: 12.5px; }
.csip-btn--lg { height: 46px; padding: 0 24px; font-size: 14px; }
.csip-btn--block { display: flex; width: 100%; }
.csip-btn[disabled], .csip-btn.is-disabled, .csip-btn[aria-disabled="true"] {
    background: var(--c-disabled-bg); color: var(--c-disabled-ink); border-color: transparent;
    cursor: not-allowed; pointer-events: none; box-shadow: none;
}
.csip-btn--secondary[disabled], .csip-btn--secondary.is-disabled, .csip-btn--secondary[aria-disabled="true"] {
    background: transparent; border-color: var(--c-rule);
}

/* ---------- Text links ---------- */
.csip-link { color: var(--c-teal); font-weight: 700; text-decoration: none; }
.csip-link:hover { text-decoration: underline; text-underline-offset: 3px; }
html.dark .csip-link { color: var(--c-teal-text); }
.csip-link--muted { color: var(--c-ink-2); font-weight: 600; }

/* ---------- Cards ---------- */
.csip-card { background: var(--c-surface); border: 1px solid var(--c-rule); border-radius: var(--r-lg); padding: 20px; color: var(--c-ink); }
.csip-card--flat { background: transparent; }
.csip-card--band { background: var(--c-band); border-color: transparent; }
.csip-card--interactive { transition: border-color .15s, box-shadow .15s; }
.csip-card--interactive:hover { border-color: var(--c-rule-2); box-shadow: 0 6px 18px -10px rgba(27, 42, 65, 0.35); }
.csip-remark { border-left: 3px solid var(--c-teal); background: var(--c-teal-tint); padding: 14px 18px; border-radius: 0 var(--r-md) var(--r-md) 0; color: var(--c-ink); }
.csip-remark--warn { border-left-color: var(--c-rust); background: var(--c-rust-tint); }
.csip-remark--accent { border-left-color: var(--c-amber); background: var(--c-amber-tint); }

/* ---------- Badges ---------- */
.csip-badge {
    display: inline-flex; align-items: center; gap: 4px; height: 22px; padding: 0 8px; border-radius: var(--r-sm);
    font: 700 10.5px/1 var(--f-sans); letter-spacing: 0.02em; white-space: nowrap;
    background: var(--c-band); color: var(--c-ink-2);
}
.csip-badge i { font-size: 9px; }
.csip-badge--done { background: var(--c-teal-tint); color: var(--c-teal-text); }
.csip-badge--accent { background: var(--c-amber-tint); color: var(--c-amber-text); }
.csip-badge--warn { background: var(--c-rust-tint); color: var(--c-rust); }
.csip-badge--outline { background: transparent; box-shadow: inset 0 0 0 1px var(--c-rule-2); }

/* ---------- Chips (filters / pickers) ---------- */
.csip-chip {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    height: 34px; padding: 0 14px; border-radius: var(--r-sm);
    border: 1px solid var(--c-rule-2); background: transparent; color: var(--c-ink-2);
    font: 600 12.5px/1 var(--f-sans); text-decoration: none; cursor: pointer; white-space: nowrap;
}
.csip-chip:hover { border-color: var(--c-ink-3); color: var(--c-ink); }
.csip-chip.is-selected, .csip-chip[aria-pressed="true"], .csip-chip[aria-selected="true"] {
    background: var(--c-teal-tint); border-color: var(--c-teal); color: var(--c-teal-text); font-weight: 700;
}
html.dark .csip-chip.is-selected, html.dark .csip-chip[aria-pressed="true"], html.dark .csip-chip[aria-selected="true"] { border-color: var(--c-teal-text); }
.csip-choice.is-selected { background: var(--c-teal); border-color: var(--c-teal); color: var(--c-on-teal); }

/* ---------- Progress ---------- */
.csip-progress { height: 6px; border-radius: 999px; background: var(--c-rule); overflow: hidden; }
.csip-progress__fill { height: 100%; background: var(--c-teal); border-radius: inherit; }
