/* ============================================================================
   theme.css — the shell: top bar, nav rail, summary cards, sheets, toasts.
   Mobile-first, designed at 390px and enhanced upward. Requires tokens.css.
   Part of the unified design system. The system is split across three files so
   that module iframes can take the tokens and the remaps WITHOUT inheriting the
   shell's own resets (* { box-sizing }, html/body margins, overflow rules),
   which would otherwise reflow every ported tool's layout.

     tokens.css        the palette and scales. loaded by the shell AND by every module.
     theme.css         shell chrome only. loaded by index.html.
     module-theme.css  per-module token remaps. loaded by every module.
   ============================================================================ */

/* ==========================================================================
   Shell — mobile-first. Designed at 390px, enhanced upward.
   ========================================================================== */

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow-x: hidden;          /* belt-and-braces against horizontal overflow */
}

body.hd-shell {
  background: var(--hd-bg);
  color: var(--hd-text);
  font-family: var(--hd-font-body);
  font-size: var(--hd-fs-base);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}

/* --- top bar ------------------------------------------------------------- */

.hd-topbar {
  position: fixed;
  inset: 0 0 auto 0;
  z-index: 40;
  height: var(--hd-nav-h);
  display: flex;
  align-items: center;
  gap: var(--hd-s3);
  padding: 0 var(--hd-s4);
  background: var(--hd-glass-strong);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--hd-border);
}

.hd-brand {
  display: flex;
  align-items: baseline;
  gap: var(--hd-s2);
  font-family: var(--hd-font-display);
  font-size: var(--hd-fs-md);
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--hd-text);
  text-decoration: none;
  white-space: nowrap;
  min-width: 0;
}
.hd-brand b { color: var(--hd-brass); font-weight: 600; }
.hd-brand span {
  font-family: var(--hd-font-mono);
  font-size: var(--hd-fs-xs);
  color: var(--hd-text-mute);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.hd-topbar-spacer { flex: 1 1 auto; }

/* --- buttons ------------------------------------------------------------- */

.hd-btn {
  appearance: none;
  border: 1px solid var(--hd-border);
  background: var(--hd-surface-2);
  color: var(--hd-text-dim);
  font-family: var(--hd-font-body);
  font-size: var(--hd-fs-sm);
  font-weight: 500;
  padding: var(--hd-s2) var(--hd-s3);
  border-radius: var(--hd-r-sm);
  cursor: pointer;
  min-height: 36px;
  display: inline-flex;
  align-items: center;
  gap: var(--hd-s2);
  white-space: nowrap;
  transition: background .15s, border-color .15s, color .15s;
}
.hd-btn:hover  { background: var(--hd-surface-3); color: var(--hd-text); border-color: var(--hd-brass-dim); }
.hd-btn:active { transform: translateY(1px); }
.hd-btn:focus-visible { outline: 2px solid var(--hd-brass); outline-offset: 2px; }
.hd-btn[disabled] { opacity: .45; cursor: not-allowed; }

.hd-btn-primary {
  background: var(--hd-brass);
  border-color: var(--hd-brass);
  color: #16180F;
  font-weight: 600;
}
.hd-btn-primary:hover { background: var(--hd-brass-soft); border-color: var(--hd-brass-soft); color: #16180F; }

.hd-btn-icon { padding: var(--hd-s2); min-width: 36px; justify-content: center; }
.hd-btn-danger:hover { border-color: var(--hd-rust); color: var(--hd-rust); }

/* --- sync status pill ---------------------------------------------------- */

.hd-sync {
  display: inline-flex;
  align-items: center;
  gap: var(--hd-s2);
  font-family: var(--hd-font-mono);
  font-size: var(--hd-fs-xs);
  color: var(--hd-text-mute);
  white-space: nowrap;
}
.hd-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--hd-text-mute);
  flex: 0 0 auto;
}
.hd-dot[data-state="ok"]      { background: var(--hd-moss); }
.hd-dot[data-state="dirty"]   { background: var(--hd-amber); }
.hd-dot[data-state="error"]   { background: var(--hd-rust); }
.hd-dot[data-state="syncing"] { background: var(--hd-steel); animation: hd-pulse 1s ease-in-out infinite; }
.hd-dot[data-state="locked"]  { background: var(--hd-brass-dim); }
@keyframes hd-pulse { 0%,100% { opacity: 1; } 50% { opacity: .25; } }

/* --- side rail (desktop) / bottom tabs (mobile) -------------------------- */

.hd-rail {
  position: fixed;
  z-index: 30;
  background: var(--hd-surface);
  border-color: var(--hd-border);
}

/* mobile: horizontal scrolling tab strip pinned to the bottom */
.hd-rail {
  inset: auto 0 0 0;
  height: 60px;
  border-top: 1px solid var(--hd-border);
  display: flex;
  align-items: stretch;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  padding: 0 var(--hd-s2);
  padding-bottom: env(safe-area-inset-bottom, 0);
}
.hd-rail::-webkit-scrollbar { display: none; }

.hd-navitem {
  appearance: none;
  border: 0;
  background: none;
  color: var(--hd-text-mute);
  font-family: var(--hd-font-body);
  font-size: var(--hd-fs-xs);
  font-weight: 500;
  padding: var(--hd-s2) var(--hd-s3);
  min-width: 64px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  cursor: pointer;
  border-radius: var(--hd-r-sm);
  text-align: center;
  flex: 0 0 auto;
  line-height: 1.15;
}
.hd-navitem .hd-glyph { font-size: 17px; line-height: 1; }
.hd-navitem:hover { color: var(--hd-text-dim); }
.hd-navitem[aria-current="page"] { color: var(--hd-brass); }
.hd-navitem:focus-visible { outline: 2px solid var(--hd-brass); outline-offset: -2px; }

/* --- main region --------------------------------------------------------- */

.hd-main {
  position: fixed;
  inset: var(--hd-nav-h) 0 60px 0;   /* below topbar, above tab strip */
  overflow: hidden;
}

.hd-view { height: 100%; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.hd-view[hidden] { display: none; }

/* the module frame fills the region entirely; each module keeps its own scroll */
.hd-frame {
  display: block;
  width: 100%;
  height: 100%;
  border: 0;
  background: var(--hd-bg);
}

/* --- home: summary cards ------------------------------------------------- */

.hd-home { padding: var(--hd-s4); padding-bottom: var(--hd-s7); }

.hd-home-head { margin: 0 0 var(--hd-s4); }
.hd-home-head h1 {
  font-family: var(--hd-font-display);
  font-size: var(--hd-fs-xl);
  font-weight: 600;
  letter-spacing: -0.02em;
  margin: 0 0 var(--hd-s1);
}
.hd-home-head p {
  margin: 0;
  color: var(--hd-text-mute);
  font-size: var(--hd-fs-sm);
}

.hd-grid {
  display: grid;
  grid-template-columns: 1fr;          /* single column at 390px */
  gap: var(--hd-s3);
}

.hd-card {
  appearance: none;
  text-align: left;
  width: 100%;
  background: var(--hd-surface);
  border: 1px solid var(--hd-border);
  border-radius: var(--hd-r);
  padding: var(--hd-s4);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: var(--hd-s2);
  box-shadow: var(--hd-shadow);
  transition: border-color .15s, transform .15s, background .15s;
  min-height: 116px;
  color: inherit;
  font: inherit;
}
.hd-card:hover { border-color: var(--hd-brass-dim); background: var(--hd-surface-2); }
.hd-card:active { transform: translateY(1px); }
.hd-card:focus-visible { outline: 2px solid var(--hd-brass); outline-offset: 2px; }

.hd-card-top {
  display: flex;
  align-items: center;
  gap: var(--hd-s2);
  min-width: 0;
}
.hd-card-glyph {
  font-size: 15px;
  width: 26px; height: 26px;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  border-radius: var(--hd-r-sm);
  background: var(--hd-surface-3);
  border: 1px solid var(--hd-border);
}
.hd-card-name {
  font-family: var(--hd-font-body);
  font-size: var(--hd-fs-sm);
  font-weight: 600;
  letter-spacing: 0.01em;
  color: var(--hd-text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.hd-card-value {
  font-family: var(--hd-font-display);
  font-size: var(--hd-fs-2xl);
  font-weight: 600;
  line-height: 1.05;
  letter-spacing: -0.02em;
  color: var(--hd-text);
  overflow-wrap: anywhere;
}
.hd-card-value[data-tone="pos"]     { color: var(--hd-moss); }
.hd-card-value[data-tone="neg"]     { color: var(--hd-rust); }
.hd-card-value[data-tone="brass"]   { color: var(--hd-brass); }
.hd-card-value[data-tone="empty"]   { color: var(--hd-text-mute); font-size: var(--hd-fs-md); }

.hd-card-label {
  font-family: var(--hd-font-mono);
  font-size: var(--hd-fs-xs);
  color: var(--hd-text-mute);
  letter-spacing: 0.03em;
  text-transform: uppercase;
}

.hd-bar {
  height: 4px;
  border-radius: 2px;
  background: var(--hd-surface-3);
  overflow: hidden;
}
.hd-bar i {
  display: block;
  height: 100%;
  background: var(--hd-grad);
  border-radius: 2px;
}

/* --- sheets / dialogs used by the shell (sync, settings) ----------------- */

.hd-sheet {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(0, 0, 0, .62);
  padding: 0;
}
.hd-sheet[hidden] { display: none; }
.hd-sheet-inner {
  width: 100%;
  max-width: 560px;
  max-height: 88vh;
  overflow-y: auto;
  background: var(--hd-surface);
  border: 1px solid var(--hd-border);
  border-radius: var(--hd-r-lg) var(--hd-r-lg) 0 0;
  padding: var(--hd-s5) var(--hd-s4);
  padding-bottom: calc(var(--hd-s5) + env(safe-area-inset-bottom, 0px));
  box-shadow: var(--hd-shadow-lg);
}
.hd-sheet h2 {
  font-family: var(--hd-font-display);
  font-size: var(--hd-fs-lg);
  font-weight: 600;
  margin: 0 0 var(--hd-s1);
  letter-spacing: -0.01em;
}
.hd-sheet .hd-sub {
  color: var(--hd-text-mute);
  font-size: var(--hd-fs-sm);
  margin: 0 0 var(--hd-s4);
}
.hd-sheet label {
  display: block;
  font-size: var(--hd-fs-xs);
  font-family: var(--hd-font-mono);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--hd-text-mute);
  margin: var(--hd-s4) 0 var(--hd-s2);
}
.hd-input {
  width: 100%;
  background: var(--hd-bg);
  border: 1px solid var(--hd-border);
  border-radius: var(--hd-r-sm);
  color: var(--hd-text);
  font-family: var(--hd-font-body);
  font-size: var(--hd-fs-base);   /* >=16px: stops iOS zoom-on-focus */
  padding: var(--hd-s3);
  min-height: 44px;
}
.hd-input:focus { outline: none; border-color: var(--hd-brass); }
.hd-row { display: flex; gap: var(--hd-s2); flex-wrap: wrap; margin-top: var(--hd-s4); }
.hd-row .hd-btn { flex: 1 1 auto; justify-content: center; }

.hd-note {
  font-size: var(--hd-fs-sm);
  color: var(--hd-text-dim);
  background: var(--hd-surface-2);
  border: 1px solid var(--hd-border);
  border-left: 2px solid var(--hd-brass-dim);
  border-radius: var(--hd-r-sm);
  padding: var(--hd-s3);
  margin: var(--hd-s3) 0 0;
}
.hd-note[data-tone="warn"] { border-left-color: var(--hd-amber); }
.hd-note[data-tone="error"] { border-left-color: var(--hd-rust); color: var(--hd-rust); }
.hd-note[data-tone="ok"]   { border-left-color: var(--hd-moss); }
.hd-note code { font-family: var(--hd-font-mono); font-size: var(--hd-fs-xs); }

.hd-conflict {
  display: grid;
  gap: var(--hd-s2);
  margin-top: var(--hd-s3);
}
.hd-conflict-opt {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--hd-s3);
  background: var(--hd-surface-2);
  border: 1px solid var(--hd-border);
  border-radius: var(--hd-r-sm);
  padding: var(--hd-s3);
  font-size: var(--hd-fs-sm);
}
.hd-conflict-opt b { display: block; font-size: var(--hd-fs-xs); font-family: var(--hd-font-mono); color: var(--hd-text-mute); text-transform: uppercase; }

/* --- toast --------------------------------------------------------------- */

.hd-toast {
  position: fixed;
  left: 50%;
  bottom: calc(60px + var(--hd-s4));
  transform: translateX(-50%);
  z-index: 80;
  background: var(--hd-surface-3);
  border: 1px solid var(--hd-border);
  border-radius: var(--hd-r-sm);
  padding: var(--hd-s3) var(--hd-s4);
  font-size: var(--hd-fs-sm);
  box-shadow: var(--hd-shadow-lg);
  max-width: calc(100vw - var(--hd-s6));
  text-align: center;
}
.hd-toast[hidden] { display: none; }

/* ==========================================================================
   Enhance upward. 768px: two-column cards. 1024px: side rail.
   ========================================================================== */

@media (min-width: 768px) {
  .hd-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--hd-s4); }
  .hd-home { padding: var(--hd-s5); }
  .hd-home-head h1 { font-size: var(--hd-fs-2xl); }
}

@media (min-width: 1024px) {
  .hd-rail {
    inset: var(--hd-nav-h) auto 0 0;
    width: var(--hd-rail-w);
    height: auto;
    flex-direction: column;
    align-items: stretch;
    overflow-x: hidden;
    overflow-y: auto;
    border-top: 0;
    border-right: 1px solid var(--hd-border);
    padding: var(--hd-s3) var(--hd-s2);
    gap: 2px;
  }
  .hd-navitem {
    flex-direction: row;
    justify-content: flex-start;
    gap: var(--hd-s3);
    font-size: var(--hd-fs-sm);
    padding: var(--hd-s3);
    min-width: 0;
    text-align: left;
  }
  .hd-navitem[aria-current="page"] { background: var(--hd-surface-2); }
  .hd-navitem .hd-glyph { font-size: 15px; width: 18px; }
  .hd-main { inset: var(--hd-nav-h) 0 0 var(--hd-rail-w); }
  .hd-toast { bottom: var(--hd-s5); left: calc(50% + var(--hd-rail-w) / 2); }
  .hd-sheet { align-items: center; padding: var(--hd-s4); }
  .hd-sheet-inner { border-radius: var(--hd-r-lg); padding-bottom: var(--hd-s5); }
}

@media (min-width: 1440px) {
  .hd-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .hd-home { padding: var(--hd-s6); }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .001ms !important; transition-duration: .001ms !important; }
}

/* ==========================================================================
   Master password gate. Sits above everything, including the nav, because
   nothing behind it is readable until the key exists.
   ========================================================================== */

.hd-gate {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--hd-s4);
  background:
    radial-gradient(120% 80% at 50% -10%, var(--hd-surface-2) 0%, var(--hd-bg) 70%),
    var(--hd-bg);
}
.hd-gate[hidden] { display: none; }

.hd-gate-inner {
  width: 100%;
  max-width: 420px;
  background: var(--hd-surface);
  border: 1px solid var(--hd-border);
  border-radius: var(--hd-r-lg);
  padding: var(--hd-s5) var(--hd-s4);
  box-shadow: var(--hd-shadow-lg);
  max-height: 92vh;
  overflow-y: auto;
}

.hd-gate-mark {
  font-size: 26px;
  color: var(--hd-brass);
  line-height: 1;
  margin-bottom: var(--hd-s3);
}

.hd-gate h1 {
  font-family: var(--hd-font-display);
  font-size: var(--hd-fs-xl);
  font-weight: 600;
  letter-spacing: -0.02em;
  margin: 0 0 var(--hd-s2);
}

.hd-gate .hd-sub {
  color: var(--hd-text-dim);
  font-size: var(--hd-fs-sm);
  margin: 0 0 var(--hd-s4);
  line-height: 1.5;
}

.hd-gate .hd-input { margin-bottom: var(--hd-s2); }
.hd-gate .hd-card-label { margin: 0 0 var(--hd-s2); }
