/* OpenSquilla Web UI — Base styles & CSS custom properties.
   Aesthetic: "mantis brutalist" — hairline-driven, tabular, decisive.
   Orange #F56600 (sampled from the mantis-shrimp mark) is reserved as
   signal: active nav, primary CTA, focus rings, status flips. */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  /* Typography */
  --font-sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: "JetBrains Mono", "SF Mono", "Fira Code", "Fira Mono", Menlo, Consolas, monospace;
  --font-display: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --fs-2xs: 0.6875rem;
  --fs-xs: 0.75rem;
  --fs-sm: 0.8125rem;
  --fs-md: 0.9375rem;
  --fs-lg: 1.125rem;
  --fs-xl: 1.5rem;
  --fs-2xl: 2rem;
  /* Display rung — the dashboard view-title size, tokenized (was hand-typed in 10 views). */
  --fs-display: clamp(1.625rem, 1.2rem + 1vw, 2.25rem);
  --lh-display: 1.05;

  --track-tabular: 0.01em;

  /* Radius */
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-xl: 16px;

  /* Spacing */
  --sp-1: 4px;
  --sp-2: 8px;
  --sp-3: 12px;
  --sp-4: 16px;
  --sp-5: 20px;
  --sp-6: 24px;
  --sp-8: 32px;

  /* Layout */
  --chat-measure: clamp(40rem, 70vw, 51.25rem);

  /* Transitions */
  --transition: 150ms ease;
  --ease-press: cubic-bezier(.2,.7,.2,1);
  --dur-entrance: 340ms;
  --stagger-step: 40ms;
  --focus-ring: color-mix(in srgb, var(--accent) 22%, transparent);

  /* Shadows */
  --shadow-color: rgba(0,0,0,0.1);
  --shadow-xs: 0 1px 2px 0 var(--shadow-color);
  --shadow-sm: 0 1px 3px 0 var(--shadow-color), 0 1px 2px -1px var(--shadow-color);
  --shadow-md: 0 4px 6px -1px var(--shadow-color), 0 2px 4px -2px var(--shadow-color);
  --shadow-lg: 0 10px 15px -3px var(--shadow-color), 0 4px 6px -4px var(--shadow-color);
  --shadow-xl: 0 20px 25px -5px var(--shadow-color), 0 8px 10px -6px var(--shadow-color);

  /* Atmospheric grain — shared between themes, layered very low. */
  --grain: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='160' height='160' viewBox='0 0 160 160'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0  0 0 0 0 0  0 0 0 0 0  0 0 0 0.55 0'/></filter><rect width='100%25' height='100%25' filter='url(%23n)'/></svg>");
}

/* Utility */
.hidden { display: none !important; }

/* Dark theme — neutral grays with mantis-orange signal (default) */
[data-theme="dark"], :root {
  --bg: #08080A;
  --bg-surface: #101013;
  --bg-elevated: #18181D;
  --bg-hover: #232328;
  --text: #ECECEF;
  --text-muted: #A1A1AA;
  --text-dim: #82828A;
  --border: rgba(255,255,255,0.07);
  --border-strong: rgba(255,255,255,0.13);
  --border-focus: rgba(255,255,255,0.18);
  --card: #101013;
  --hairline: rgba(255,255,255,0.06);

  --accent: #F56600;
  --accent-hover: #D9580E;
  --accent-deep: #B0440A;
  --accent-secondary: #FF8A4C;
  --accent-foreground: #1a0e02;
  --ok: #22C55E;
  --warn: #EAB308;
  --danger: #F87171;
  --info: #60A5FA;
  --shadow: rgba(0, 0, 0, 0.5);
  --shadow-color: rgba(0,0,0,0.5);
  --grain-opacity: 0.05;

  /* Aliases */
  --surface-2: var(--bg-elevated);
  --surface-3: var(--bg-hover);
  --radius: var(--radius-md);
  --color-green: var(--ok);
  --color-red: var(--danger);
  --color-blue: var(--info);
  --text-secondary: var(--text-muted);

}

/* Light theme — paper-warm with the same hue family as the dark accent. */
[data-theme="light"] {
  --bg: #F7F6F3;
  --bg-surface: #FFFFFF;
  --bg-elevated: #F2F0EB;
  --bg-hover: #E7E4DD;
  --text: #18181B;
  --text-muted: #4B4B53;
  --text-dim: #66666E;
  --border: rgba(20,18,15,0.10);
  --border-strong: rgba(20,18,15,0.18);
  --border-focus: rgba(20,18,15,0.24);
  --card: #FFFFFF;
  --hairline: rgba(20,18,15,0.08);

  --accent: #B84404;
  --accent-hover: #A23C02;
  --accent-deep: #7A2C00;
  --accent-secondary: #DD6224;
  --accent-foreground: #FFFFFF;
  --ok: #166534;
  --warn: #7A4F00;
  --danger: #A61B1B;
  --info: #1D4ED8;
  --shadow: rgba(40,30,20,0.10);
  --shadow-color: rgba(40,30,20,0.10);
  --grain-opacity: 0.03;

  --surface-2: var(--bg-elevated);
  --surface-3: var(--bg-hover);
  --radius: var(--radius-md);
  --color-green: var(--ok);
  --color-red: var(--danger);
  --color-blue: var(--info);
  --text-secondary: var(--text-muted);

}

html, body {
  height: 100%;
  font-family: var(--font-sans);
  font-size: var(--fs-md);
  color: var(--text);
  background: var(--bg);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  transition: background-color 200ms ease, color 150ms ease;
}

/* Atmospheric grain — sits over background only, never blocks input. */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image: var(--grain);
  background-size: 160px 160px;
  opacity: var(--grain-opacity);
  mix-blend-mode: overlay;
  z-index: 0;
}

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-hover); }

input, textarea, select {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  color: var(--text);
  padding: var(--sp-2) var(--sp-3);
  font-family: var(--font-sans);
  font-size: var(--fs-sm);
  outline: none;
  transition: border-color var(--transition), box-shadow var(--transition);
}
input:focus, textarea:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
}
input::placeholder, textarea::placeholder { color: var(--text-dim); }

code, pre {
  font-family: var(--font-mono);
  font-size: var(--fs-sm);
  font-variant-ligatures: none;
}

pre {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--sp-3);
  overflow-x: auto;
}

/* Scrollbar */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 4px;
  /* Floor the thumb so a long thread can't shrink it to an ungrabbable sliver. */
  min-height: 48px;
  min-width: 48px;
}
::-webkit-scrollbar-thumb:hover { background: var(--text-dim); }
/* Firefox */
* { scrollbar-width: thin; scrollbar-color: var(--border-strong) transparent; }

/* Layout */
#app {
  display: flex;
  /* Fallback first; dynamic viewport keeps the chat composer above mobile browser chrome. */
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  position: relative;
  z-index: 1;
}

.sidebar {
  width: 220px;
  min-width: 220px;
  background:
    linear-gradient(180deg, var(--bg-surface) 0%, color-mix(in srgb, var(--bg-surface) 96%, var(--accent)) 200%),
    var(--bg-surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  transition: background-color 200ms ease, border-color 200ms ease;
  position: relative;
}
/* Sidebar — kept clean; the active-nav accent rail does the heavy lifting. */

.main {  /* Tier A: rounded top-left shoulder */
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
  /* Tier A: rounded top-left shoulder */
  border-top-left-radius: var(--radius-lg);
}

.topbar {
  height: 48px;
  min-height: 48px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--sp-4);
  background: color-mix(in srgb, var(--bg-surface) 88%, transparent);
  gap: var(--sp-3);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(10px) saturate(140%);
  -webkit-backdrop-filter: blur(10px) saturate(140%);
  position: relative;
}
/* Hairline accent rail beneath the topbar — barely-there orange thread. */
.topbar::after {
  content: "";
  position: absolute;
  left: 0; right: 0; bottom: -1px;
  height: 1px;
  background: linear-gradient(90deg, transparent 0%, color-mix(in srgb, var(--accent) 38%, transparent) 18%, transparent 42%);
  pointer-events: none;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 0 0 auto;
  min-width: 0;
}
.topbar-center {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
}
.topbar-center.hidden { display: none !important; }
.topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 0 0 auto;
  min-width: 0;
}

.approval-inline {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
  border: 1px solid color-mix(in srgb, var(--warn) 55%, var(--border));
  background: color-mix(in srgb, var(--warn) 16%, var(--bg-surface));
  color: var(--warn);
  border-radius: 999px;
  cursor: pointer;
  font-size: var(--fs-xs);
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  min-height: 28px;
  padding: 0 var(--sp-3);
  white-space: nowrap;
  position: relative;
  transition: background var(--transition), border-color var(--transition);
  animation: approval-attention 1.6s ease-in-out infinite;
}
.approval-inline::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--warn);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--warn) 30%, transparent);
}
.approval-inline:hover {
  background: color-mix(in srgb, var(--warn) 28%, var(--bg-surface));
  border-color: var(--warn);
}
@keyframes approval-attention {
  0%, 100% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--warn) 35%, transparent); }
  50% { box-shadow: 0 0 0 4px color-mix(in srgb, var(--warn) 12%, transparent); }
}

@media (max-width: 768px) {
  .approval-inline {
    justify-content: center;
    gap: 0;
    width: 34px;
    min-width: 34px;
    padding: 0;
    overflow: hidden;
    font-size: 0;
  }
}

.content {
  flex: 1;
  overflow-y: auto;
  padding: var(--sp-6);
  animation: fadeIn 220ms var(--ease-press);
  position: relative;
  z-index: 1;
}
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* Sidebar nav */
.nav-brand {
  height: 48px;
  min-height: 48px;
  padding: 0 var(--sp-4);
  color: var(--text);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 11px;
  position: relative;
  z-index: 1;
  box-sizing: border-box;
}
.nav-brand .brand-mark {
  width: 30px;
  height: 30px;
  display: block;
  flex: 0 0 auto;
  filter: drop-shadow(0 1px 5px color-mix(in srgb, var(--accent) 24%, transparent));
}
/* Hairline divider that doubles as an orange "signal" thread — echoes the
   topbar accent rail and the nav-foot dot, per the mantis-brutalist palette. */
.nav-brand__div {
  flex: 0 0 auto;
  width: 1px;
  height: 26px;
  background: linear-gradient(
    180deg,
    transparent,
    color-mix(in srgb, var(--accent) 55%, var(--border)),
    transparent
  );
}
/* Weight-split wordmark: "Open" recedes, "Squilla" (the species) anchors. */
.nav-brand__wm {
  font-size: 1.14rem;
  letter-spacing: 0;
  line-height: 1;
}
.nav-brand__pre {
  font-weight: 520;
  color: var(--text-muted);
}
.nav-brand__name {
  font-weight: 760;
  color: var(--text);
}

.nav-group-label {
  padding: var(--sp-2) var(--sp-4) 4px;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  margin-top: var(--sp-3);
  position: relative;
  z-index: 1;
  font-family: var(--font-sans);
}
.nav-group-label:first-of-type { margin-top: var(--sp-2); }

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 40px;
  padding: 7px var(--sp-3);
  color: var(--text-dim);
  cursor: pointer;
  border-radius: var(--radius-md);
  margin: 1px var(--sp-2);
  font-size: var(--fs-sm);
  font-weight: 500;
  position: relative;
  z-index: 1;
  letter-spacing: 0;
  transition: background var(--transition), color var(--transition);
}
.nav-item:hover { background: var(--bg-hover); color: var(--text); }
.nav-item:hover svg { opacity: 0.85; }

.nav-item.is-active {
  background: linear-gradient(
    90deg,
    color-mix(in srgb, var(--accent) 20%, var(--bg-surface)) 0%,
    color-mix(in srgb, var(--accent) 4%, var(--bg-surface)) 100%
  );
  color: var(--text);
  font-weight: 600;
}
.nav-item.is-active::before {
  content: "";
  position: absolute;
  left: -8px;
  top: 5px;
  bottom: 5px;
  width: 3px;
  background: var(--accent);
  border-radius: 0 2px 2px 0;
  box-shadow: 0 0 14px color-mix(in srgb, var(--accent) 65%, transparent);
}
.nav-item.is-active:hover {
  background: linear-gradient(
    90deg,
    color-mix(in srgb, var(--accent) 26%, var(--bg-surface)) 0%,
    color-mix(in srgb, var(--accent) 8%, var(--bg-surface)) 100%
  );
}
.nav-item svg { width: 16px; height: 16px; opacity: 0.65; transition: opacity var(--transition), color var(--transition); }
.nav-item.is-active svg { opacity: 1; color: var(--accent); }
.nav-item:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

@media (min-width: 769px) and (max-height: 640px) {
  .nav-brand {
    height: 44px;
    min-height: 44px;
  }

  .nav-group-label {
    margin-top: var(--sp-2);
    padding: 5px var(--sp-4) 2px;
    font-size: 9.5px;
    line-height: 1.1;
  }

  .nav-group-label:first-of-type {
    margin-top: 4px;
  }
}

/* Sidebar brand footer — small identity anchor at the bottom of the rail.
   margin-top: auto pushes it to the bottom whether the nav list is long or
   short; the small accent dot doubles as a discreet liveness cue so the
   brand reads alive without competing with the nav-active accent rail. */
.nav-foot {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: auto;
  padding: var(--sp-3) var(--sp-4) calc(var(--sp-3) + env(safe-area-inset-bottom, 0px));
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.06em;
  color: var(--text-dim);
  border-top: 1px solid var(--border);
  background: linear-gradient(180deg, transparent 0%, color-mix(in srgb, var(--accent) 4%, transparent) 100%);
  user-select: none;
}
.nav-foot__dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 6px color-mix(in srgb, var(--accent) 50%, transparent);
  flex-shrink: 0;
  animation: nav-foot-pulse 2.4s ease-in-out infinite;
}
.nav-foot__ver {
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
}
@keyframes nav-foot-pulse {
  0%, 100% { box-shadow: 0 0 4px color-mix(in srgb, var(--accent) 35%, transparent); }
  50%      { box-shadow: 0 0 10px color-mix(in srgb, var(--accent) 65%, transparent); }
}
@media (prefers-reduced-motion: reduce) {
  .nav-foot__dot { animation: none; }
}

/* Approval count badge */
.nav-badge {
  margin-left: auto;
  min-width: 20px;
  height: 20px;
  padding: 0 7px;
  border-radius: 999px;
  background: var(--warn);
  color: #1a0e02;
  font-size: 10.5px;
  font-weight: 800;
  line-height: 20px;
  letter-spacing: 0.02em;
  text-align: center;
  font-variant-numeric: tabular-nums;
  font-family: var(--font-mono);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--warn) 25%, transparent);
}

/* Sidebar toggle — hidden on desktop */
.sidebar-toggle.btn { display: none; }

/* Accessibility: improved color contrast (WCAG AA) */
[data-theme="dark"] {
  --text-muted: #A8A8B0;
  --text-dim: #82828A;
}

/* View headers — baseline accent rule. */
.view-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: var(--sp-3);
  margin-bottom: var(--sp-4);
  position: relative;
}

/* ─── Reduced Motion ──────────────────────────────────────────────────── */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
