:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-subtle: #eef2f6;
  --header-bg: #0f172a;
  --header-fg: #f8fafc;
  --header-muted: #94a3b8;
  --ink: #0f172a;
  --muted: #475569;
  --border: #e2e8f0;
  --accent: #2563eb;
  --accent-soft: #eff6ff;
  --accent-fg: #ffffff;
  --rail-tint: rgba(15, 23, 42, 0.04);
  --danger: #dc2626;
  --ok: #059669;
  --warn: #d97706;
  --duo-right-rail: 0px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --ease-out: cubic-bezier(0.23, 1, 0.32, 1);
  color-scheme: light;
}

html.theme-dark {
  --bg: #0b1120;
  --surface: #131c31;
  --surface-subtle: #1b2742;
  --header-bg: #070c18;
  --header-fg: #f8fafc;
  --header-muted: #94a3b8;
  --ink: #f1f5f9;
  --muted: #94a3b8;
  --border: #24324f;
  --accent: #3b82f6;
  --accent-soft: rgba(59, 130, 246, 0.16);
  --accent-fg: #ffffff;
  --rail-tint: rgba(255, 255, 255, 0.035);
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

html {
  margin: 0;
  padding: 0;
  background: var(--header-bg);
  color: var(--ink);
  font: 15px/1.55 -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Segoe UI", sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-x: none;
}

body {
  margin: 0;
  padding: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--ink);
  overscroll-behavior-x: none;
  overflow-x: hidden;
}

html[data-duo-wide="on"],
html[data-duo-wide="on"] body {
  width: var(--duo-target-width, 951px);
  height: var(--duo-target-height, 669px);
  overflow: hidden;
  touch-action: pan-y;
}

button,
a,
input,
label {
  font: inherit;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.app-shell {
  position: relative;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

html[data-duo-wide="on"] .app-shell {
  width: var(--duo-target-width, 951px);
  height: var(--duo-target-height, 669px);
  min-height: 0;
  overflow: hidden;
}

/* Full-bleed decorative right rail backdrop when Duo wide mode is active */
.app-shell::after {
  content: "";
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: var(--duo-right-rail);
  background: var(--rail-tint);
  border-left: 1px solid var(--border);
  pointer-events: none;
  z-index: 5;
  opacity: 0;
  transition: opacity 180ms var(--ease-out);
}

html[data-duo-wide="on"] .app-shell::after {
  opacity: 1;
}

/* Header spans full 951pt edge-to-edge; interactive nav stays left of the 84pt rail */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  flex-shrink: 0;
  width: 100%;
  background: var(--header-bg);
  color: var(--header-fg);
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  padding: calc(10px + var(--safe-top)) calc(16px + var(--duo-right-rail) + var(--safe-right)) 10px calc(16px + var(--safe-left));
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  color: var(--header-fg);
  margin-right: 4px;
}

.brand-mark {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  background: linear-gradient(135deg, #38bdf8, #2563eb);
  color: #fff;
  font-weight: 700;
  font-size: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  letter-spacing: -0.02em;
  flex-shrink: 0;
}

.brand-text {
  display: flex;
  flex-direction: column;
  line-height: 1.2;
}

.brand-title {
  font-size: 15px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.brand-sub {
  font-size: 11px;
  color: var(--header-muted);
}

.topnav {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-left: auto;
}

.topnav a {
  color: #cbd5e1;
  text-decoration: none;
  padding: 7px 12px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  min-height: 36px;
  display: inline-flex;
  align-items: center;
  transition: background 150ms var(--ease-out), color 150ms var(--ease-out), transform 140ms var(--ease-out);
}

.topnav a:active {
  transform: scale(0.97);
}

.topnav a[aria-current="page"] {
  background: rgba(255, 255, 255, 0.14);
  color: #ffffff;
}

.topbar-tools {
  display: flex;
  align-items: center;
  gap: 6px;
}

.chip-btn {
  min-height: 34px;
  padding: 0 11px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.22);
  background: rgba(255, 255, 255, 0.06);
  color: #f8fafc;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  transition: background 150ms var(--ease-out), transform 140ms var(--ease-out);
}

.chip-btn:active {
  transform: scale(0.97);
}

.chip-btn.active {
  background: #2563eb;
  border-color: #3b82f6;
  color: #ffffff;
}

/* Full-bleed hero strip demonstrating edge-to-edge fill behind the right rail */
.hero-strip {
  flex-shrink: 0;
  width: 100%;
  background: linear-gradient(100deg, #162447 0%, #1d3557 62%, #1e40af 100%);
  color: #f8fafc;
  padding: 20px calc(20px + var(--duo-right-rail) + var(--safe-right)) 20px calc(20px + var(--safe-left));
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.hero-inner {
  max-width: 820px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.hero-kicker {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: #93c5fd;
  margin-bottom: 4px;
}

.hero-strip h1 {
  margin: 0 0 6px;
  font-size: clamp(20px, 2.8vw, 26px);
  letter-spacing: -0.025em;
  line-height: 1.25;
}

.hero-strip p {
  margin: 0;
  font-size: 13px;
  color: #cbd5e1;
  max-width: 60ch;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 6px 12px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.12);
  border: 1px solid rgba(255, 255, 255, 0.18);
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #34d399;
}

.status-dot.off {
  background: #fbbf24;
}

/* Main workspace keeps interactive content left of the 84pt rail */
.main-content {
  flex: 1;
  min-height: 0;
  padding: 20px calc(20px + var(--duo-right-rail) + var(--safe-right)) 28px calc(20px + var(--safe-left));
}

html[data-duo-wide="on"] .main-content {
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
}

.page-wrap {
  max-width: 820px;
}

.grid-2 {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  margin-bottom: 14px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 18px;
  margin-bottom: 14px;
}

.card h2 {
  margin: 0 0 6px;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -0.015em;
}

.card p.desc {
  margin: 0 0 14px;
  font-size: 13px;
  color: var(--muted);
}

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.btn {
  min-height: 40px;
  padding: 0 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  text-decoration: none;
  transition: background 150ms var(--ease-out), border-color 150ms var(--ease-out), transform 140ms var(--ease-out);
}

.btn:active {
  transform: scale(0.97);
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-fg);
}

.btn.subtle {
  background: var(--surface-subtle);
}

.btn.danger {
  color: var(--danger);
  border-color: rgba(220, 38, 38, 0.35);
}

.metric-row {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.metric-box {
  background: var(--surface-subtle);
  border-radius: 10px;
  padding: 12px;
}

.metric-label {
  font-size: 11px;
  color: var(--muted);
  margin-bottom: 3px;
}

.metric-value {
  font: 700 16px/1.25 ui-monospace, SFMono-Regular, Menlo, monospace;
  color: var(--ink);
}

.counter-num {
  font: 750 42px/1.1 ui-rounded, -apple-system, sans-serif;
  color: var(--accent);
  margin: 4px 0 12px;
}

/* Tasks page */
.task-form {
  display: flex;
  gap: 8px;
  margin-bottom: 14px;
}

.task-form input[type="text"] {
  flex: 1;
  min-width: 0;
  min-height: 42px;
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
}

.task-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.task-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}

.task-item:last-child {
  border-bottom: none;
}

.task-item label {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 36px;
  cursor: pointer;
}

.task-item input[type="checkbox"] {
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
  flex-shrink: 0;
}

.task-item input:checked + span {
  text-decoration: line-through;
  color: var(--muted);
}

/* Reader page */
.article-body {
  font-size: 15px;
  line-height: 1.75;
}

.article-body.large {
  font-size: 18px;
  line-height: 1.8;
}

.article-body h3 {
  margin: 18px 0 8px;
  font-size: 17px;
}

.article-body p {
  margin: 0 0 12px;
  color: var(--ink);
}

.callout {
  background: var(--accent-soft);
  border-left: 3px solid var(--accent);
  border-radius: 0 10px 10px 0;
  padding: 12px 14px;
  margin: 14px 0;
  font-size: 13px;
}

/* Diagnostics table & log */
.kv-list {
  display: grid;
  grid-template-columns: 160px minmax(0, 1fr);
  gap: 8px 12px;
  font-size: 13px;
  margin: 0;
}

.kv-list dt {
  color: var(--muted);
}

.kv-list dd {
  margin: 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  overflow-wrap: anywhere;
}

.event-log {
  margin: 10px 0 0;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--surface-subtle);
  font: 12px/1.55 ui-monospace, SFMono-Regular, Menlo, monospace;
  max-height: 180px;
  overflow-y: auto;
  white-space: pre-wrap;
}

/* Footer status bar */
.footerbar {
  flex-shrink: 0;
  width: 100%;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 9px calc(16px + var(--duo-right-rail) + var(--safe-right)) calc(9px + var(--safe-bottom)) calc(16px + var(--safe-left));
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  font: 12px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
  color: var(--muted);
}

/* Boundary ruler overlay (toggled on/off) */
#duo-ruler {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  z-index: 50;
  border: 3px solid #22c55e;
}

#duo-ruler[hidden] {
  display: none;
}

.ruler-rail {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: 84px;
  background: rgba(239, 68, 68, 0.16);
  border-left: 2px dashed #f59e0b;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 6px;
  text-align: center;
  color: #dc2626;
  font: 700 11px/1.35 ui-monospace, monospace;
}

html.theme-dark .ruler-rail {
  color: #fca5a5;
  background: rgba(239, 68, 68, 0.22);
}

.ruler-badge {
  position: absolute;
  bottom: 58px;
  right: 92px;
  background: rgba(15, 23, 42, 0.9);
  color: #34d399;
  padding: 4px 8px;
  border-radius: 6px;
  font: 600 11px/1.3 ui-monospace, monospace;
}

#toast {
  position: fixed;
  top: calc(64px + var(--safe-top));
  right: calc(16px + var(--duo-right-rail) + var(--safe-right));
  z-index: 60;
  background: #0f172a;
  color: #ffffff;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 13px;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.28);
  pointer-events: none;
}

#toast[hidden] {
  display: none;
}

#safe-probe {
  position: fixed;
  visibility: hidden;
  pointer-events: none;
  padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
}

@media (max-width: 640px) {
  .grid-2,
  .metric-row {
    grid-template-columns: 1fr;
  }
  .topnav {
    order: 3;
    width: 100%;
    margin-left: 0;
  }
  .kv-list {
    grid-template-columns: 120px minmax(0, 1fr);
  }
}
