/* ZERO TWO adaptive layer: dark mode + any screen size.
   Loaded LAST. It fixes two things the earlier layers could not:
   1. The phone stylesheets (mobile-aevy / mobile-brand / mobile-content) hard-code light colours with
      !important, so dark mode only worked on tablets and desktops. Section A repaints them from the
      theme variables.
   2. Sizing was tuned for a few widths only. Section B makes every size from 280px phones to
      ultrawide monitors fluid, safe-area aware and overflow-proof. */

/* =====================================================================
   A. THEME FOUNDATION
   ===================================================================== */
:root { color-scheme: light; }
html[data-theme="dark"] { color-scheme: dark; }

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; -webkit-tap-highlight-color: transparent; }
html[data-theme="dark"] { background: #1f1138; }
html[data-theme="light"] { background: #f9f5ff; }

/* One dark palette on every size: style.css forced near-black (#050507) on tablet/desktop while phones and
   the header were navy. Same specificity as that rule, loaded later, so this wins. */
:root[data-theme="dark"] {
  --bg: #1f1138; --bg2: #291748; --surface: rgba(45,26,78,.94); --surface2: #35205b; --surface3: #462d74;
  --line: #40296c; --line2: #523a7f; --line3: #6c529e;
  --text: #f5f0ff; --text2: #d3c1f4; --muted: #b19bdc;
}
html[data-theme="dark"] body { background-color: #1f1138; }
html[data-theme="dark"] .aevy-word { color: #f3edff !important; }
html[data-theme="dark"] .aevy-flower { color: #b88eff !important; }

/* Smooth theme switch, but never on first paint and never for reduced-motion users */
html.theme-anim, html.theme-anim *, html.theme-anim *::before, html.theme-anim *::after {
  transition: background-color .25s ease, border-color .25s ease, color .2s ease, fill .2s ease, stroke .2s ease !important;
}
@media (prefers-reduced-motion: reduce) {
  html.theme-anim, html.theme-anim *, html.theme-anim *::before, html.theme-anim *::after { transition: none !important; }
}

/* Native controls, scrollbars, selection, autofill */
html[data-theme="dark"] ::selection { background: rgba(184,142,255,.38); color: #ffffff; }
html[data-theme="light"] ::selection { background: rgba(126,61,239,.22); }
html[data-theme="dark"] * { scrollbar-color: #523a7f transparent; }
html[data-theme="light"] * { scrollbar-color: #d0b6ff transparent; }
html[data-theme="dark"] input:-webkit-autofill,
html[data-theme="dark"] textarea:-webkit-autofill,
html[data-theme="dark"] select:-webkit-autofill {
  -webkit-text-fill-color: var(--text, #f5f0ff);
  caret-color: var(--text, #f5f0ff);
  -webkit-box-shadow: 0 0 0 100px #35205b inset;
}
html[data-theme="dark"] input::placeholder,
html[data-theme="dark"] textarea::placeholder { color: #a58ecf; opacity: 1; }
html[data-theme="dark"] input, html[data-theme="dark"] textarea, html[data-theme="dark"] select { caret-color: #c8a9ff; }

/* Keyboard focus that reads in both themes */
:focus-visible { outline: 2px solid var(--accent, #8446ee); outline-offset: 2px; }
html[data-theme="dark"] :focus-visible { outline-color: #c8a9ff; }

/* Photos and avatars: calm the glare in dark, keep logos/icons untouched */
html[data-theme="dark"] .post img, html[data-theme="dark"] .ig-post img,
html[data-theme="dark"] .pp-card img, html[data-theme="dark"] .ap-hero img,
html[data-theme="dark"] .gw img, html[data-theme="dark"] .card img { filter: brightness(.92) contrast(1.02); }
html[data-theme="dark"] img[src$=".svg"], html[data-theme="dark"] .avatar img { filter: none; }

/* Code blocks stay readable in both themes */
html[data-theme="dark"] pre, html[data-theme="dark"] code, html[data-theme="dark"] .code {
  background-color: #170c2b; color: #eadfff; border-color: #40296c;
}
html[data-theme="light"] pre, html[data-theme="light"] code { color: #3a1d6b; }

/* Dark: toasts and menus must not be a white slab */
html[data-theme="dark"] .toast, html[data-theme="dark"] #toast { background: #3b2466; color: #f5f0ff; border: 1px solid #523a7f; }

/* =====================================================================
   A2. DARK MODE ON PHONES (<=760px)
   Re-paints every hard-coded light surface from the earlier phone layers.
   ===================================================================== */
@media (max-width: 760px) {
  html[data-theme="dark"] { --bln: #412a6c; --bsoft: #2e1b50; --bk: #d8c0ff; --bk2: #b88eff; }

  /* canvas */
  html[data-theme="dark"] body {
    background: linear-gradient(180deg, #291748 0, #1f1138 360px) #1f1138 !important;
    color: var(--text, #f5f0ff);
  }

  /* header */
  html[data-theme="dark"] .site-header, html[data-theme="dark"] .site-header.hide {
    background: rgba(31,17,56,.92) !important; border-bottom: 1px solid #412a6c !important;
    box-shadow: 0 8px 24px -18px rgba(0,0,0,.7) !important;
  }
  html[data-theme="dark"] .site-header .brand .aevy-flower { color: #b88eff !important; }
  html[data-theme="dark"] .site-header .brand .aevy-word { color: #f3edff !important; }
  html[data-theme="dark"] .site-header .bar-actions .mobile-alerts,
  html[data-theme="dark"] .site-header .bar-actions .mobile-profile,
  html[data-theme="dark"] .site-header .bar-actions .hd-search,
  html[data-theme="dark"] .site-header .bar-actions .msg-top,
  html[data-theme="dark"] .site-header .bar-actions .burger {
    background: #301b57 !important; border: 1px solid #4b3379 !important; color: #dfceff !important;
  }
  html[data-theme="dark"] .site-header .bar-actions .mobile-alerts::after { border-color: #301b57; }

  /* home hero text */
  html[data-theme="dark"] .ax-dash-copy .ax-eyebrow { color: #ccaeff; }
  html[data-theme="dark"] .ax-dash-copy h1 span { color: #ccaeff; }
  html[data-theme="dark"] .ax-dash-actions .btn.btn-primary { background: #f4eeff !important; color: #501ba8 !important; }
  html[data-theme="dark"] .ax-dash-actions .btn.ax-ghost { background: rgba(255,255,255,.14) !important; color: #ffffff !important; border-color: rgba(255,255,255,.35) !important; }

  /* cards, panels, rows */
  html[data-theme="dark"] .ax-dash-shortcuts a,
  html[data-theme="dark"] .ax-dash-panel,
  html[data-theme="dark"] .ax-dash-tip,
  html[data-theme="dark"] .ax-dash-library .ax-row,
  html[data-theme="dark"] .ax-dash-library .ax-empty,
  html[data-theme="dark"] .ig-post,
  html[data-theme="dark"] .ap-search,
  html[data-theme="dark"] .ap-res,
  html[data-theme="dark"] .pp-card,
  html[data-theme="dark"] .pp-about,
  html[data-theme="dark"] .pp-complete,
  html[data-theme="dark"] footer {
    background: #2d1a4e !important; border-color: #412a6c !important;
    box-shadow: 0 10px 26px -18px rgba(0,0,0,.65) !important;
  }
  /* tile keeps its light chip so the blue background-image icon stays visible (only the colour changes, never the image) */
  html[data-theme="dark"] .ax-dash-shortcuts i { background-color: #efe6ff !important; border-color: #523a7f !important; box-shadow: none !important; }
  html[data-theme="dark"] .ax-dash-shortcuts em { color: #ccaeff; border-color: #523a7f; }

  /* chips / filters / selects */
  html[data-theme="dark"] .ax-filter button,
  html[data-theme="dark"] .chip,
  html[data-theme="dark"] .ap-chip,
  html[data-theme="dark"] .sc-sort select, html[data-theme="dark"] #sort {
    background: #301b57 !important; border-color: #4b3379 !important; color: #dfceff !important;
  }
  html[data-theme="dark"] .ax-filter button.on,
  html[data-theme="dark"] .chip.on, html[data-theme="dark"] .chip[aria-pressed="true"],
  html[data-theme="dark"] .ap-chip.on {
    background: #b88eff !important; border-color: #b88eff !important; color: #1d0f36 !important;
  }

  /* tab bar */
  html[data-theme="dark"] .tabbar {
    background: rgba(37,20,68,.94) !important; border-color: rgba(175,145,230,.28) !important;
    box-shadow: 0 12px 32px -14px rgba(0,0,0,.7) !important;
  }
  html[data-theme="dark"] .tabbar a, html[data-theme="dark"] .tabbar a > b { color: #ceb9f5 !important; }
  html[data-theme="dark"] .tabbar a[aria-current="page"] > svg { background: #462d74 !important; color: #eadfff !important; }
  html[data-theme="dark"] .tabbar a[aria-current="page"] > b { color: #ffffff !important; }

  /* headings and text that were navy-on-light */
  html[data-theme="dark"] .ax-dash-panel h3,
  html[data-theme="dark"] .ax-dash-library .ax-lib-head h2,
  html[data-theme="dark"] .ax-dash-shortcuts b,
  html[data-theme="dark"] .page-head h1,
  html[data-theme="dark"] .ig-post .post-name, html[data-theme="dark"] .ig-post .ig-title h3,
  html[data-theme="dark"] .ap-shelf > header h2, html[data-theme="dark"] .ap-t, html[data-theme="dark"] .ap-res b,
  html[data-theme="dark"] .pp-card .pp-id h1, html[data-theme="dark"] .pp-card.ig .ig-head .pp-id h1,
  html[data-theme="dark"] .pp-card .pf-stats b, html[data-theme="dark"] .pp-about h3,
  html[data-theme="dark"] .dlg-head h2, html[data-theme="dark"] .up-head h2 { color: #f5f0ff !important; }

  html[data-theme="dark"] .ax-dash-shortcuts small,
  html[data-theme="dark"] .page-head p, html[data-theme="dark"] .sc-count,
  html[data-theme="dark"] .ig-post .ig-desc, html[data-theme="dark"] .ap-m,
  html[data-theme="dark"] .pp-card .pf-un, html[data-theme="dark"] .pp-card .pf-stats span, html[data-theme="dark"] .pp-meta { color: #b19bdc !important; }

  html[data-theme="dark"] .ax-trends a, html[data-theme="dark"] .ax-dash-tip p,
  html[data-theme="dark"] .ig-post .pact, html[data-theme="dark"] .pp-card .ig-bio p,
  html[data-theme="dark"] .pp-bio, html[data-theme="dark"] .pp-ver { color: #d3c1f4 !important; }
  html[data-theme="dark"] .ax-trends a { border-color: #3f266b !important; }
  html[data-theme="dark"] .ax-trends a span, html[data-theme="dark"] .ap-more,
  html[data-theme="dark"] .ap-tabs button.on, html[data-theme="dark"] .pp-card .ig-tabs button.on { color: #ccaeff !important; }
  html[data-theme="dark"] .ap-tabs, html[data-theme="dark"] .pp-card .ig-tabs, html[data-theme="dark"] .pp-card .pp-tabs { border-color: #412a6c !important; }
  html[data-theme="dark"] .ig-post .pact.on { color: #ff7a80; }

  html[data-theme="dark"] .ig-post .ig-tags span,
  html[data-theme="dark"] .pp-card .pf-stats li,
  html[data-theme="dark"] .pp-card .ig-tabs small { background: #3f266b !important; color: #d8c0ff !important; }
  html[data-theme="dark"] .pp-card .ig-ring .avatar, html[data-theme="dark"] .pp-card .ig-ring img { border-color: #2d1a4e !important; }

  /* dialogs, sheets, upload, popovers */
  html[data-theme="dark"] dialog, html[data-theme="dark"] .up-box,
  html[data-theme="dark"] .mp, html[data-theme="dark"] .pwa-card, html[data-theme="dark"] .dx {
    background: #291748 !important; color: #f5f0ff; border-color: #412a6c !important;
  }
  html[data-theme="dark"] dialog::backdrop { background: rgba(10,4,22,.66); }
  html[data-theme="dark"] .dlg-head { border-color: #412a6c !important; }
  html[data-theme="dark"] .dlg-head::before, html[data-theme="dark"] .up-box::before { background: #523a7f !important; }
  html[data-theme="dark"] .dlg-head .icon-btn, html[data-theme="dark"] #dlgClose,
  html[data-theme="dark"] .up-head .icon-btn { background: #3f266b !important; color: #dfceff !important; border-color: #523a7f !important; }
  html[data-theme="dark"] .up-box .drop { background: #301b57 !important; color: #d3c1f4 !important; border-color: #523a7f !important; }
  html[data-theme="dark"] .up-box .field, html[data-theme="dark"] .up-box input, html[data-theme="dark"] .up-box select {
    background: #301b57 !important; color: #f5f0ff !important; border-color: #523a7f !important;
  }
  html[data-theme="dark"] .ap-hero .ap-get.big { background: #f4eeff !important; color: #481b8f !important; }
}

/* Light cards that sit in tablet / desktop dark mode keep working (pastel-theme handles them);
   only make sure form fields everywhere follow the theme. */
html[data-theme="dark"] .field, html[data-theme="dark"] input[type="text"], html[data-theme="dark"] input[type="search"],
html[data-theme="dark"] input[type="email"], html[data-theme="dark"] input[type="password"],
html[data-theme="dark"] input[type="url"], html[data-theme="dark"] textarea, html[data-theme="dark"] select {
  background-color: #301b57; color: #f5f0ff; border-color: #523a7f;
}
html[data-theme="dark"] select option { background: #301b57; color: #f5f0ff; }

/* Optional true-black mode for OLED phones: add data-contrast="oled" on <html> */
html[data-theme="dark"][data-contrast="oled"] body { background: #000000 !important; }
html[data-theme="dark"][data-contrast="oled"] .site-header,
html[data-theme="dark"][data-contrast="oled"] .tabbar { background: rgba(0,0,0,.92) !important; }

/* Increased-contrast preference */
@media (prefers-contrast: more) {
  html[data-theme="dark"] { --line: #6c529e; --line2: #866bb8; --muted: #d3c1f4; }
  html[data-theme="light"] { --line: #bc9ff0; --line2: #a37fe3; --muted: #5b408d; }
}

/* =====================================================================
   B. ANY SCREEN SIZE
   ===================================================================== */

/* B1. Never let anything push the page sideways, on any device */
html, body { max-width: 100%; overflow-x: clip; }
img, svg, video, canvas, iframe, embed, object { max-width: 100%; }
img, video { height: auto; }
.avatar img, .ig-ring img, img[width][height] { height: revert-layer; }
main, .view, .wrap, .card, .post, .ig-post, .side-card, .pp-card { min-width: 0; }
h1, h2, h3, h4, p, li, a, span, label, .post-name, .ig-desc, .ig-title { overflow-wrap: break-word; }
pre, .code { max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { display: block; max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }

/* B2. Safe areas: notches, dynamic island, punch-hole cameras, landscape cut-outs, foldable hinges */
@supports (padding: max(0px)) {
  .site-header { padding-left: env(safe-area-inset-left, 0px); padding-right: env(safe-area-inset-right, 0px); }
  .tabbar { margin-left: max(0px, env(safe-area-inset-left, 0px)); margin-right: max(0px, env(safe-area-inset-right, 0px)); }
  main > .view > .wrap, footer .wrap {
    padding-left: max(0px, env(safe-area-inset-left, 0px)); padding-right: max(0px, env(safe-area-inset-right, 0px));
  }
}

/* B3. Real viewport height on mobile browsers (address bar collapsing) */
@supports (height: 100dvh) {
  dialog, .up-box, .mp { max-height: min(92dvh, 100% - 8px); }
  .view { min-height: 60dvh; }
}
dialog, .up-box { overscroll-behavior: contain; }

/* B4. Touch ergonomics: 44px targets and no hover-only affordances on touch screens */
@media (hover: none) and (pointer: coarse) {
  .btn, .icon-btn, .chip, .ap-chip, .tabbar a, .nav a, .pact, button[class*="btn"], select, input[type="checkbox"] + label {
    min-height: 44px;
  }
  .icon-btn { min-width: 44px; }
  input, select, textarea { font-size: max(16px, 1em); }   /* stops iOS zooming into fields */
  a, button { touch-action: manipulation; }
}

/* B5. Very small phones (<=340px: Galaxy Fold cover screen, old iPhone SE, small Androids) */
@media (max-width: 340px) {
  main > .view > .wrap, footer .wrap, .ax-in { width: calc(100% - 20px) !important; }
  .site-header .bar-actions { gap: 4px !important; }
  .site-header .brand .aevy-word { font-size: 18px !important; }
  .site-header .bar-actions .icon-btn { width: 36px !important; height: 36px !important; min-width: 36px; }
  .ax-dash-shortcuts { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; }
  .ax-dash-copy h1 { font-size: clamp(1.9rem, 11vw, 2.4rem) !important; }
  .tabbar a > b { font-size: 9px !important; }
  .btn { padding-inline: 12px; }
}

/* B6. Home shortcut tiles: labels were clipping on 375px phones. Let them wrap instead. */
@media (max-width: 760px) {
  .ax-dash-shortcuts { grid-template-columns: repeat(auto-fit, minmax(76px, 1fr)); }
  .ax-dash-shortcuts a { min-width: 0; padding-inline: 6px !important; }
  .ax-dash-shortcuts a > span { min-width: 0; max-width: 100%; }
  .ax-dash-shortcuts b { max-width: 100%; white-space: normal !important; text-align: center; line-height: 1.15 !important; overflow-wrap: break-word; hyphens: auto; font-size: clamp(10px, 2.9vw, 12.5px) !important; }
}

/* B7. Phones in landscape: short viewport, so shrink chrome and hero */
@media (max-height: 480px) and (orientation: landscape) {
  .site-header, .site-header.hide { height: 48px !important; }
  .tabbar { transform: scale(.9); transform-origin: bottom center; }
  body { padding-bottom: calc(64px + env(safe-area-inset-bottom, 0px)) !important; }
  #homeRoot .ax-dash-hero, .ax-dash-hero { height: auto !important; min-height: 0 !important; }
  .ax-dash-copy { padding: 18px 22px !important; }
  .ax-dash-copy h1 { font-size: clamp(1.6rem, 5vw, 2.2rem) !important; }
  dialog, .up-box { max-height: 96dvh; }
}

/* B8. Large phones, foldables and small tablets (481-760px): use the extra width */
@media (min-width: 481px) and (max-width: 760px) {
  .cards, #cards { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; gap: 14px; }
  .ax-dash-shortcuts { grid-template-columns: repeat(4, minmax(0, 1fr)) !important; }
  #homeRoot .ax-dash-hero { height: clamp(420px, 72vw, 520px) !important; }
}

/* B9. Tablets (761-1023px): comfortable reading width, 2-3 column grids, bigger type */
@media (min-width: 761px) and (max-width: 1023px) {
  .cards, #cards { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; }
  .ax-dash-shortcuts { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  main > .view > .wrap { width: min(100% - 48px, 860px); }
  dialog, .up-box { width: min(640px, calc(100vw - 48px)); margin-inline: auto; }
}

/* B10. Laptops and desktops: stop content stretching on huge or ultrawide screens */
@media (min-width: 1280px) {
  .cards, #cards { grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); }
}
@media (min-width: 1600px) {
  :root { --wrap-max: 1480px; }
  main > .view > .wrap, .ax-in, footer .wrap { max-width: var(--wrap-max); margin-inline: auto; }
  html { font-size: 17px; }
}
@media (min-width: 2200px) {
  :root { --wrap-max: 1760px; }
  html { font-size: 19px; }
}
@media (min-width: 3000px) { html { font-size: 22px; } }   /* 4K / TV browsers */

/* B11. Fold / dual-screen devices: keep content off the hinge */
@media (horizontal-viewport-segments: 2) {
  main > .view > .wrap { padding-inline: calc(env(viewport-segment-width 0 0, 50%) * 0 + 8px); }
}

/* B12. Short desktop windows: sidebar must scroll, not clip */
@media (min-width: 1024px) and (max-height: 640px) {
  .side, aside, #nav { overflow-y: auto; overscroll-behavior: contain; }
}

/* B12b. Phone size floor: earlier sheets stacked !important rules that shrank text to 9-10px and left small
   tap targets. This comes last and sets readable minimums without touching the layout. */
@media (max-width: 760px) {
  .tabbar a > b { font-size: 11px !important; letter-spacing: 0 !important; }
  .ax-dash-copy .ax-eyebrow, .ax-dash-library .ax-eyebrow, .ax-eyebrow { font-size: 11px !important; }
  .ax-dash-shortcuts b { font-size: clamp(11.5px, 3.2vw, 13px) !important; }
  .ax-dash-copy > p:not(.ax-eyebrow) { font-size: max(14px, 3.7vw) !important; }
  .ax-all, .ax-dash-library a, .ax-dash-panel a { display: inline-flex; align-items: center; min-height: 44px; }
  .site-header .brand { min-height: 44px; display: inline-flex; align-items: center; }
  .site-header .bar-actions .icon-btn, .site-header .bar-actions .mobile-alerts, .site-header .bar-actions .mobile-profile { min-width: 40px; min-height: 44px; }
}
@media (max-width: 340px) {
  .tabbar a > b { font-size: 10.5px !important; }
  .site-header .bar-actions .icon-btn { width: 40px !important; height: 44px !important; min-width: 40px !important; }
}

/* B12c. AI page avatars: force the photo to fill its circle. (Ishita's own avatars live in ishita.css.) */
.ai-av, .ai-top b i { overflow: hidden; border-radius: 50%; }
.ai-av img, .ai-top b i img {
  width: 100% !important; height: 100% !important; max-width: none; aspect-ratio: 1 / 1;
  object-fit: cover; object-position: 50% 25%; border-radius: 50% !important; display: block;
}
.ai-av { width: 34px; height: 34px; aspect-ratio: 1 / 1; }
.ai-av { width: 34px; height: 34px; aspect-ratio: 1 / 1; }

/* B13. Print: always light and ink-friendly */
@media print {
  html, body { background: #ffffff !important; color: #000000 !important; }
  .site-header, .tabbar, .dashboard-topbar, .side { display: none !important; }
}

/* =====================================================================
   C. DESKTOP / TABLET DARK DETAILS
   ===================================================================== */
html[data-theme="dark"] .dashboard-topbar form,
html[data-theme="dark"] .ds-pill, html[data-theme="dark"] .ap-search {
  background: #301b57 !important; border-color: #523a7f !important;
}
html[data-theme="dark"] .dashboard-topbar input, html[data-theme="dark"] .ds-pill input { background: transparent !important; color: #f5f0ff !important; }
html[data-theme="dark"] .dashboard-topbar form svg, html[data-theme="dark"] .ds-ico { stroke: #ccaeff; color: #ccaeff; }
html[data-theme="dark"] .dashboard-topbar kbd { background: #3f266b; color: #d3c1f4; border-color: #523a7f; }
