/* ============================================================
   OP-1 Field companion — dark, mobile-first, system fonts only.
   No external fonts or CDNs (reference view must work offline).
   ============================================================ */

:root {
  /* Encoder accent colours, tuned for contrast on near-black. */
  --c-blue:   #3f86d6;
  --c-ochre:  #d39a3a;
  --c-gray:   #aeb4ba;
  --c-orange: #ef6a37;

  /* Surfaces */
  --bg:        #0a0a0b;
  --bg-raised: #161618;
  --bg-card:   #1c1c1f;
  --bg-chip:   #242428;
  --border:    #2c2c31;
  --border-strong: #3a3a41;

  /* Text */
  --fg:        #f1f1f3;
  --fg-dim:    #aeaeb6;
  --fg-faint:  #74747e;

  --accent:    #ef6a37;
  --radius:    12px;
  --tap:       44px;

  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --tabbar-h: calc(58px + var(--safe-bottom));

  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
    Arial, "Apple Color Emoji", "Segoe UI Emoji", sans-serif;
}

* { box-sizing: border-box; }

/* Ensure the `hidden` attribute always wins over class-level `display` rules
   (e.g. .view / .shortcut / .tip set display:flex, which would otherwise
   out-specify the UA [hidden] { display:none } and leave elements visible). */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  -webkit-text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

body { font-size: 16px; line-height: 1.5; }

/* Respect light mode if the OS asks — but keep it readable, not blinding. */
@media (prefers-color-scheme: light) {
  :root {
    --bg:        #f4f4f5;
    --bg-raised: #ffffff;
    --bg-card:   #ffffff;
    --bg-chip:   #ececed;
    --border:    #dcdce0;
    --border-strong: #c6c6cc;
    --fg:        #17171a;
    --fg-dim:    #4c4c54;
    --fg-faint:  #8a8a92;
    --c-blue:   #1f5fab;
    --c-ochre:  #9a6c14;
    --c-gray:   #5d636a;
    --c-orange: #c8451b;
  }
}

#app {
  min-height: 100dvh;
  max-width: 720px;
  margin: 0 auto;
}

button { font-family: inherit; cursor: pointer; }

/* ---------------------------------------------------------------
   Views
   --------------------------------------------------------------- */
.view {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  /* leave room for the fixed bottom tab bar */
  padding-bottom: var(--tabbar-h);
}

/* ---------------------------------------------------------------
   Sticky search
   --------------------------------------------------------------- */
.search-bar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 14px calc(12px + env(safe-area-inset-top, 0px));
  padding-top: max(12px, env(safe-area-inset-top, 0px));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

#search {
  flex: 1;
  min-height: var(--tap);
  padding: 10px 14px;
  font-size: 16px; /* >=16px stops iOS zoom-on-focus */
  color: var(--fg);
  background: var(--bg-chip);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  outline: none;
}
#search::placeholder { color: var(--fg-faint); }
#search:focus { border-color: var(--accent); }

#search-clear {
  min-width: var(--tap);
  min-height: var(--tap);
  font-size: 16px;
  color: var(--fg-dim);
  background: var(--bg-chip);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
}

/* ---------------------------------------------------------------
   Results / groups / cards
   --------------------------------------------------------------- */
.results { padding: 8px 14px 24px; }

.group { margin-top: 18px; }
.group:first-child { margin-top: 8px; }

.group-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin: 0 2px 10px;
  position: sticky;
  top: calc(var(--tap) + 24px);
  z-index: 10;
}
.group-title {
  margin: 0;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--fg-dim);
}
.group-count { font-size: 0.72rem; color: var(--fg-faint); }

.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 13px 14px;
  margin-bottom: 10px;
}
.card-title {
  margin: 0 0 2px;
  font-size: 1.02rem;
  font-weight: 650;
  letter-spacing: -0.01em;
}
.card-note {
  margin: 6px 0 0;
  font-size: 0.86rem;
  color: var(--fg-dim);
}
.card-meta {
  margin: 8px 0 0;
  font-size: 0.8rem;
  color: var(--fg-faint);
}
.card-meta b { color: var(--fg-dim); font-weight: 600; }

/* Getting-started walkthroughs (collapsible, numbered) */
.walk > summary {
  list-style: none;
  display: flex;
  align-items: flex-start;
  gap: 10px;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
}
.walk > summary::-webkit-details-marker { display: none; }
.walk-num {
  flex: 0 0 auto;
  width: 26px;
  height: 26px;
  display: grid;
  place-items: center;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 16%, transparent);
  border-radius: 8px;
}
.walk-head { flex: 1; min-width: 0; }
.walk-title {
  display: block;
  font-weight: 650;
  font-size: 1rem;
  letter-spacing: -0.01em;
}
.walk-goal {
  display: block;
  margin-top: 2px;
  font-size: 0.84rem;
  color: var(--fg-dim);
}
.walk-chevron {
  flex: 0 0 auto;
  color: var(--fg-faint);
  font-size: 1.35rem;
  line-height: 1;
  transform: rotate(90deg);
  transition: transform 0.15s ease;
}
.walk[open] > summary .walk-chevron { transform: rotate(270deg); }

.steps {
  margin: 13px 0 2px;
  padding: 0;
  list-style: none;
  counter-reset: step;
}
.steps li {
  position: relative;
  padding-left: 32px;
  margin-bottom: 10px;
  font-size: 0.9rem;
  line-height: 1.45;
  counter-increment: step;
}
.steps li:last-child { margin-bottom: 0; }
.steps li::before {
  content: counter(step);
  position: absolute;
  left: 0;
  top: -1px;
  width: 22px;
  height: 22px;
  display: grid;
  place-items: center;
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--fg-dim);
  background: var(--bg-chip);
  border: 1px solid var(--border);
  border-radius: 50%;
}

/* Encoder chips: four colour-coded params */
.encoders {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-top: 10px;
}
.layer-label {
  grid-column: 1 / -1;
  margin: 6px 0 0;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--fg-faint);
}
.chip {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 38px;
  padding: 6px 10px;
  background: var(--bg-chip);
  border: 1px solid var(--border);
  border-left-width: 3px;
  border-radius: 8px;
  font-size: 0.86rem;
  line-height: 1.25;
}
.chip .dot {
  flex: 0 0 auto;
  width: 9px;
  height: 9px;
  border-radius: 50%;
}
.chip .val { color: var(--fg); }
.chip-blue   { border-left-color: var(--c-blue); }
.chip-ochre  { border-left-color: var(--c-ochre); }
.chip-gray   { border-left-color: var(--c-gray); }
.chip-orange { border-left-color: var(--c-orange); }
.chip-blue   .dot { background: var(--c-blue); }
.chip-ochre  .dot { background: var(--c-ochre); }
.chip-gray   .dot { background: var(--c-gray); }
.chip-orange .dot { background: var(--c-orange); }

/* Shortcut rows */
.shortcut {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 8px;
}
.shortcut.is-tip { border-color: color-mix(in srgb, var(--c-orange) 45%, var(--border)); }
.shortcut.is-warning { border-color: color-mix(in srgb, var(--c-orange) 60%, var(--border)); }
.shortcut-main { min-width: 0; }
.shortcut-action {
  font-size: 0.92rem;
  font-weight: 550;
}
.shortcut-ctx {
  font-size: 0.74rem;
  color: var(--fg-faint);
  margin-top: 2px;
}
.shortcut-ctx .warn { color: var(--c-orange); font-weight: 600; }
.combo {
  flex: 0 0 auto;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--fg);
  background: var(--bg-chip);
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  padding: 5px 9px;
  white-space: nowrap;
  text-align: right;
  max-width: 48%;
}

/* Plain key/value list (tape tricks, specs, io pages…) */
.kv {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 12px;
  margin: 8px 0 0;
  font-size: 0.86rem;
}
.kv dt { color: var(--fg-dim); font-weight: 600; }
.kv dd { margin: 0; color: var(--fg); }

.taglist {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}
.tag {
  font-size: 0.78rem;
  color: var(--fg-dim);
  background: var(--bg-chip);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 10px;
}

.tip {
  display: flex;
  gap: 10px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-left: 3px solid var(--c-orange);
  border-radius: 10px;
  padding: 11px 13px;
  margin-bottom: 8px;
  font-size: 0.9rem;
}
.tip .bulb { flex: 0 0 auto; }

.no-results {
  text-align: center;
  color: var(--fg-faint);
  padding: 32px 16px;
}

/* ---------------------------------------------------------------
   Ask view
   --------------------------------------------------------------- */
#view-ask { flex: 1; }
.chat {
  flex: 1;
  overflow-y: auto;
  padding: 16px 14px 8px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.chat-intro {
  color: var(--fg-dim);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.chat-intro h2 { margin: 0 0 6px; font-size: 1.05rem; color: var(--fg); }
.chat-intro p { margin: 0; font-size: 0.9rem; }

.msg {
  max-width: 90%;
  padding: 10px 13px;
  border-radius: 14px;
  font-size: 0.95rem;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg-user {
  align-self: flex-end;
  background: var(--c-blue);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.msg-assistant {
  align-self: flex-start;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.msg-error {
  align-self: stretch;
  max-width: 100%;
  background: color-mix(in srgb, var(--c-orange) 14%, var(--bg-card));
  border: 1px solid color-mix(in srgb, var(--c-orange) 50%, var(--border));
  color: var(--fg);
}
.msg strong { font-weight: 700; }
.msg code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.86em;
  background: var(--bg-chip);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 5px;
}
.msg-user code { background: rgba(0, 0, 0, 0.25); border-color: transparent; }
.msg .cursor {
  display: inline-block;
  width: 7px;
  height: 1.05em;
  vertical-align: -2px;
  background: var(--fg-dim);
  animation: blink 1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

.ask-form {
  position: sticky;
  bottom: 0;
  display: flex;
  gap: 8px;
  align-items: flex-end;
  padding: 10px 14px;
  padding-bottom: max(10px, var(--safe-bottom));
  background: color-mix(in srgb, var(--bg) 90%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
}
#ask-input {
  flex: 1;
  resize: none;
  max-height: 140px;
  min-height: var(--tap);
  padding: 11px 14px;
  font-size: 16px;
  line-height: 1.4;
  color: var(--fg);
  background: var(--bg-chip);
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  outline: none;
  font-family: inherit;
}
#ask-input:focus { border-color: var(--accent); }
#ask-send {
  flex: 0 0 auto;
  min-width: 64px;
  min-height: var(--tap);
  padding: 0 16px;
  font-size: 0.95rem;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  border: none;
  border-radius: 12px;
}
#ask-send:disabled { opacity: 0.5; }

/* ---------------------------------------------------------------
   Bottom tab bar
   --------------------------------------------------------------- */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  display: flex;
  max-width: 720px;
  margin: 0 auto;
  height: var(--tabbar-h);
  padding-bottom: var(--safe-bottom);
  background: color-mix(in srgb, var(--bg-raised) 96%, transparent);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--border);
}
.tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  background: none;
  border: none;
  color: var(--fg-faint);
  font-size: 0.72rem;
  letter-spacing: 0.02em;
}
.tab-glyph { font-size: 1.15rem; line-height: 1; }
.tab[aria-selected="true"] { color: var(--accent); }

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; scroll-behavior: auto !important; }
}
