/*
 * Cheiro — design system stylesheet
 *
 * Mobile-first. iPhone is the primary target. Desktop is a centered
 * 720px reading column inside a sidebar app shell.
 *
 * Step 5.3 change
 * ---------------
 * Layout offsets that depend on the chrome being visible (top bar
 * height on mobile, left rail width on desktop) are gated behind
 * `body.is-authenticated`. When the chrome is hidden — sign-in
 * screen, unlock screen, fatal error states — `#app` fills the
 * viewport edge-to-edge and the screen container centers cleanly
 * without phantom offsets from a chrome that isn't there.
 */

/* ------------------------------------------------------------------ */
/*  1. Variables                                                      */
/* ------------------------------------------------------------------ */

:root {
  --color-bg: #0c0a08;
  --color-surface: #15120e;
  --color-surface-raised: #1d1814;
  --color-surface-overlay: #221c17;
  --color-border: #2a241e;
  --color-border-strong: #3a3329;

  --color-text: #ececec;
  --color-text-muted: #9a9a9a;
  --color-text-faint: #5a5a5a;

  --color-accent: #d4b27a;
  --color-accent-soft: #8a7a55;

  --color-verdict-favorable: #7aa8c4;
  --color-verdict-neutral:   #9a9a9a;
  --color-verdict-cautionary:#d4b27a;
  --color-verdict-grave:     #a85339;

  --color-focus: var(--color-accent);

  --font-serif: 'Crimson Pro', ui-serif, Georgia, 'Times New Roman', serif;
  --font-sans: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
               'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Consolas,
               'Liberation Mono', monospace;

  --fs-display: 1.75rem;
  --fs-h1:      1.375rem;
  --fs-h2:      1.125rem;
  --fs-body:    1rem;
  --fs-reading: 1.0625rem;
  --fs-small:   0.875rem;
  --fs-tiny:    0.75rem;

  --lh-tight: 1.25;
  --lh-normal: 1.5;
  --lh-reading: 1.65;

  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.5rem;
  --space-6: 2rem;
  --space-7: 3rem;
  --space-8: 4rem;

  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;

  --tap-target: 44px;

  --top-bar-height: 52px;
  --tab-bar-height: 64px;
  --rail-width: 220px;

  --motion-fast: 150ms;
  --motion-base: 200ms;
  --motion-slow: 250ms;
  --ease-out: cubic-bezier(0.2, 0, 0, 1);
  --ease-in-out: cubic-bezier(0.4, 0, 0.2, 1);
}

/* ------------------------------------------------------------------ */
/*  2. Reset and base                                                 */
/* ------------------------------------------------------------------ */

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

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  min-height: 100%;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-sans);
  font-size: var(--fs-body);
  line-height: var(--lh-normal);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
  overscroll-behavior-y: none;
}

input, textarea, select, button { font: inherit; color: inherit; }
input, textarea, select { font-size: max(var(--fs-body), 16px); }

a {
  color: var(--color-accent);
  text-decoration: underline;
  text-underline-offset: 2px;
  text-decoration-thickness: 1px;
}

::selection { background: var(--color-accent); color: var(--color-bg); }

/* ------------------------------------------------------------------ */
/*  3. Typography                                                     */
/* ------------------------------------------------------------------ */

.title-display { font-family: var(--font-serif); font-size: var(--fs-display); font-weight: 500; line-height: var(--lh-tight); letter-spacing: 0.005em; }
.title-h1      { font-family: var(--font-serif); font-size: var(--fs-h1);      font-weight: 500; line-height: var(--lh-tight); }
.title-h2      { font-family: var(--font-serif); font-size: var(--fs-h2);      font-weight: 500; line-height: var(--lh-tight); }

.text-reading      { font-family: var(--font-serif); font-size: var(--fs-reading); line-height: var(--lh-reading); }
.text-reading p + p { margin-top: 1em; }

.text-body  { font-size: var(--fs-body);  line-height: var(--lh-normal); }
.text-small { font-size: var(--fs-small); }
.text-tiny  { font-size: var(--fs-tiny); letter-spacing: 0.02em; }

.text-muted { color: var(--color-text-muted); }
.text-faint { color: var(--color-text-faint); }

.text-mono       { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }
.text-smallcaps  { font-variant-caps: small-caps; letter-spacing: 0.06em; text-transform: lowercase; }

/* ------------------------------------------------------------------ */
/*  4. Layout                                                         */
/* ------------------------------------------------------------------ */

#app {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  width: 100%;
  min-height: 100dvh;
}

/* ------- Top bar (mobile only) ------- */

.top-bar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: calc(var(--top-bar-height) + env(safe-area-inset-top));
  padding:
    env(safe-area-inset-top)
    calc(var(--space-4) + env(safe-area-inset-right))
    0
    calc(var(--space-4) + env(safe-area-inset-left));
  background: var(--color-bg);
  border-bottom: 1px solid var(--color-border);
}

.top-bar__title {
  font-family: var(--font-serif);
  font-size: var(--fs-h1);
  font-variant-caps: small-caps;
  letter-spacing: 0.08em;
  font-weight: 500;
}

.top-bar__action {
  width: var(--tap-target);
  height: var(--tap-target);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: 0;
  color: var(--color-text);
  cursor: pointer;
  border-radius: var(--radius-md);
}
.top-bar__action:active { background: var(--color-surface-raised); }

/* ------- Screen container ------- */

.screen {
  flex: 1 1 auto;
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  padding:
    var(--space-5)
    calc(var(--space-4) + env(safe-area-inset-right))
    var(--space-5)
    calc(var(--space-4) + env(safe-area-inset-left));
}

/* When chrome is visible, screens reserve room for the bottom tab bar
   on mobile so the last item isn't hidden behind it. Desktop layout
   doesn't need this — the rail is on the side, not the bottom. */
body.is-authenticated .screen {
  padding-bottom:
    calc(var(--tab-bar-height) + env(safe-area-inset-bottom) + var(--space-5));
}

/* ------- Tab bar (mobile: bottom) / Rail (desktop: left) ------- */

.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 10;
  display: flex;
  height: calc(var(--tab-bar-height) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--color-surface);
  border-top: 1px solid var(--color-border);
}

.tab-bar__items { display: flex; flex: 1 1 auto; }

.tab-bar__rail-header,
.tab-bar__rail-footer { display: none; }

.tab-bar__item {
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-1);
  background: transparent;
  border: 0;
  color: var(--color-text-muted);
  cursor: pointer;
  text-decoration: none;
  font-size: var(--fs-tiny);
  letter-spacing: 0.02em;
  min-height: var(--tap-target);
  padding: var(--space-2);
}

.tab-bar__item[aria-current='page'] { color: var(--color-accent); }

.tab-bar__item svg { width: 22px; height: 22px; flex: 0 0 auto; }

/* ------- Desktop layout ------- */

@media (min-width: 900px) {
  body { flex-direction: row; }
  .top-bar { display: none; }

  .tab-bar {
    position: fixed;
    top: 0;
    left: 0;
    right: auto;
    bottom: 0;
    width: var(--rail-width);
    height: 100vh;
    height: 100dvh;
    flex-direction: column;
    align-items: stretch;
    justify-content: flex-start;
    padding: 0;
    border-top: 0;
    border-right: 1px solid var(--color-border);
  }

  .tab-bar__rail-header {
    display: flex;
    align-items: center;
    padding: var(--space-5) var(--space-5) var(--space-4);
    border-bottom: 1px solid var(--color-border);
    flex: 0 0 auto;
  }

  .tab-bar__wordmark {
    font-family: var(--font-serif);
    font-size: var(--fs-h1);
    font-variant-caps: small-caps;
    letter-spacing: 0.08em;
    font-weight: 500;
    color: var(--color-text);
  }

  .tab-bar__items {
    flex: 1 1 auto;
    flex-direction: column;
    align-items: stretch;
    padding: var(--space-3) var(--space-2);
    gap: var(--space-1);
    overflow-y: auto;
  }

  .tab-bar__item {
    flex: 0 0 auto;
    flex-direction: row;
    justify-content: flex-start;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-4);
    font-size: var(--fs-body);
    border-radius: var(--radius-md);
    min-height: var(--tap-target);
  }

  .tab-bar__item:hover { background: var(--color-surface-raised); }
  .tab-bar__item[aria-current='page'] { background: var(--color-surface-raised); }

  .tab-bar__rail-footer {
    display: flex;
    flex: 0 0 auto;
    border-top: 1px solid var(--color-border);
    padding: var(--space-3) var(--space-2);
  }

  .tab-bar__rail-action {
    flex: 1 1 auto;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: var(--space-3);
    background: transparent;
    border: 0;
    color: var(--color-text-muted);
    cursor: pointer;
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-md);
    font-size: var(--fs-body);
  }

  .tab-bar__rail-action:hover { background: var(--color-surface-raised); color: var(--color-text); }

  .tab-bar__rail-action svg { width: 22px; height: 22px; }

  /* Apply the rail offset to #app ONLY when authenticated. Otherwise
     the unauthenticated screens (sign-in, unlock, fatal) get the
     full viewport and center cleanly. */
  body.is-authenticated #app {
    margin-left: var(--rail-width);
    width: calc(100% - var(--rail-width));
    min-height: 100dvh;
  }

  body.is-authenticated .screen {
    padding: var(--space-6) var(--space-5);
    margin: 0 auto;
  }

  /* When NOT authenticated on desktop: full viewport, no rail offset. */
  body:not(.is-authenticated) #app {
    margin-left: 0;
    width: 100%;
  }
  body:not(.is-authenticated) .screen {
    padding: var(--space-6) var(--space-5);
    margin: 0 auto;
  }
}

/* ------------------------------------------------------------------ */
/*  5. Components                                                     */
/* ------------------------------------------------------------------ */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  background: var(--color-surface-raised);
  color: var(--color-text);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-md);
  padding: var(--space-3) var(--space-5);
  min-height: var(--tap-target);
  cursor: pointer;
  touch-action: manipulation;
  transition: background var(--motion-fast) var(--ease-out);
  font-size: var(--fs-body);
  letter-spacing: 0.01em;
}
.btn:active { background: var(--color-border); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn--primary {
  background: var(--color-accent);
  color: var(--color-bg);
  border-color: var(--color-accent);
  font-weight: 600;
}
.btn--primary:active { background: var(--color-accent-soft); border-color: var(--color-accent-soft); }

.btn--ghost { background: transparent; border-color: transparent; }
.btn--ghost:active { background: var(--color-surface-raised); }

.btn--danger { color: var(--color-verdict-grave); border-color: var(--color-verdict-grave); }
.btn--danger:active { background: rgba(168, 83, 57, 0.15); }

.btn--full { width: 100%; }

.input {
  display: block;
  width: 100%;
  background: var(--color-surface);
  color: var(--color-text);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-3);
  -webkit-appearance: none;
  appearance: none;
  min-height: var(--tap-target);
  transition: border-color var(--motion-fast) var(--ease-out);
}
.input:focus { outline: none; border-color: var(--color-accent); }

textarea.input {
  resize: vertical;
  min-height: calc(var(--tap-target) * 2);
  line-height: var(--lh-normal);
  font-family: var(--font-mono);
}

.field { display: flex; flex-direction: column; gap: var(--space-2); }
.field__label { font-size: var(--fs-small); color: var(--color-text-muted); letter-spacing: 0.02em; }
.field__error { font-size: var(--fs-small); color: var(--color-verdict-grave); }

.card { background: var(--color-surface); border: 1px solid var(--color-border); border-radius: var(--radius-lg); padding: var(--space-5); }
.card + .card { margin-top: var(--space-4); }

.divider { height: 1px; background: var(--color-border); margin: var(--space-5) 0; border: 0; }

.divider--ornament {
  height: var(--space-5);
  margin: var(--space-6) 0;
  border: 0;
  background: none;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--color-accent-soft);
  font-family: var(--font-serif);
  letter-spacing: 0.4em;
}
.divider--ornament::before { content: "\002767"; font-size: 1.25rem; }

.sheet {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  animation: fade-in var(--motion-slow) var(--ease-out);
}

.sheet__panel {
  background: var(--color-surface);
  border-top-left-radius: var(--radius-lg);
  border-top-right-radius: var(--radius-lg);
  padding:
    var(--space-5)
    calc(var(--space-5) + env(safe-area-inset-right))
    calc(var(--space-5) + env(safe-area-inset-bottom))
    calc(var(--space-5) + env(safe-area-inset-left));
  animation: slide-up var(--motion-slow) var(--ease-out);
  max-height: 90dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

@media (min-width: 600px) {
  .sheet { justify-content: center; align-items: center; padding: var(--space-5); }
  .sheet__panel {
    border-radius: var(--radius-lg);
    width: 100%;
    max-width: 480px;
    animation: fade-in var(--motion-slow) var(--ease-out);
  }
}

/* ------------------------------------------------------------------ */
/*  6. Verdict utilities                                              */
/* ------------------------------------------------------------------ */

.verdict-favorable { color: var(--color-verdict-favorable); }
.verdict-neutral   { color: var(--color-verdict-neutral); }
.verdict-cautionary{ color: var(--color-verdict-cautionary); }
.verdict-grave     { color: var(--color-verdict-grave); }

.verdict-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  vertical-align: middle;
  margin-right: var(--space-2);
}
.verdict-dot--favorable  { background: var(--color-verdict-favorable); }
.verdict-dot--neutral    { background: var(--color-verdict-neutral); }
.verdict-dot--cautionary { background: var(--color-verdict-cautionary); }
.verdict-dot--grave      { background: var(--color-verdict-grave); }

/* ------------------------------------------------------------------ */
/*  7. Focus + motion                                                 */
/* ------------------------------------------------------------------ */

:focus-visible { outline: 2px solid var(--color-focus); outline-offset: 2px; }

@keyframes fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

@keyframes slide-up {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
