:root {
  color-scheme: light dark;
  --bg: #f5f5f7;
  --surface: #ffffff;
  --sidebar: rgba(242, 242, 247, 0.72);
  --sidebar-solid: #f2f2f7;
  --label: #1d1d1f;
  --label-2: rgba(60, 60, 67, 0.62);
  --label-3: rgba(60, 60, 67, 0.36);
  --separator: rgba(60, 60, 67, 0.16);
  --fill: rgba(120, 120, 128, 0.12);
  --fill-2: rgba(120, 120, 128, 0.2);
  --accent: #007aff;
  --accent-soft: rgba(0, 122, 255, 0.12);
  --red: #ff3b30;
  --green: #34c759;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 8px 24px rgba(0, 0, 0, 0.06);
  --radius: 12px;
  --ease: cubic-bezier(0.25, 0.1, 0.25, 1);
  --spring: cubic-bezier(0.34, 1.3, 0.64, 1);
  --fast: 180ms;
  --snappy: 280ms;
}

@supports (color: color(display-p3 1 1 1)) {
  :root {
    --accent: color(display-p3 0 0.478 1);
  }
}

@supports (animation-timing-function: linear(0, 1)) {
  :root {
    --spring: linear(0, 0.012, 0.049, 0.107, 0.185, 0.278, 0.383, 0.493, 0.601, 0.702, 0.791, 0.865, 0.924, 0.967, 0.995, 1.013, 1.019, 1.016, 1.009, 1.003, 1);
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1c1e;
    --surface: #2c2c2e;
    --sidebar: rgba(36, 36, 38, 0.72);
    --sidebar-solid: #242426;
    --label: #f5f5f7;
    --label-2: rgba(235, 235, 245, 0.62);
    --label-3: rgba(235, 235, 245, 0.32);
    --separator: rgba(84, 84, 88, 0.5);
    --fill: rgba(120, 120, 128, 0.22);
    --fill-2: rgba(120, 120, 128, 0.32);
    --accent: #0a84ff;
    --accent-soft: rgba(10, 132, 255, 0.2);
    --red: #ff453a;
    --green: #30d158;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.3);
  }
}

@media (prefers-contrast: more) {
  :root {
    --label-2: rgba(60, 60, 67, 0.85);
    --separator: rgba(60, 60, 67, 0.5);
    --fill: rgba(120, 120, 128, 0.3);
  }
}

* { box-sizing: border-box; }
.sprite { position: absolute; }
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--label);
  font: 15px/1.47 -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, "Segoe UI", Roboto, sans-serif;
  letter-spacing: -0.01em;
  -webkit-font-smoothing: antialiased;
}

button, input { font: inherit; color: inherit; letter-spacing: inherit; }
button { cursor: pointer; }
svg { width: 1em; height: 1em; flex: none; }

:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 55%, transparent);
  outline-offset: 2px;
  border-radius: 8px;
}

.app {
  display: grid;
  grid-template-columns: minmax(300px, 340px) 1fr;
  height: 100dvh;
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 24px;
  min-height: 0;
  overflow-y: auto;
  padding: max(24px, env(safe-area-inset-top)) 16px 16px max(16px, env(safe-area-inset-left));
  background: var(--sidebar);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-right: 1px solid var(--separator);
}

@media (prefers-reduced-transparency: reduce) {
  .sidebar { background: var(--sidebar-solid); backdrop-filter: none; -webkit-backdrop-filter: none; }
}

.sidebar-head { padding: 0 8px; }
h1 {
  margin: 0;
  font: 700 28px/1.14 -apple-system, BlinkMacSystemFont, "SF Pro Display", system-ui, sans-serif;
  letter-spacing: -0.022em;
}

.offline-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 12px 0 0;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--fill);
  color: var(--label-2);
  font-size: 13px;
}
.offline-badge svg { font-size: 16px; }

.search { display: grid; gap: 12px; }
.field {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 44px;
  padding: 0 12px;
  border-radius: 12px;
  background: var(--fill);
  color: var(--label-2);
  transition: background var(--fast) var(--ease), box-shadow var(--fast) var(--ease);
}
.field:focus-within { background: var(--surface); box-shadow: 0 0 0 3px var(--accent-soft), 0 0 0 1px var(--accent); }
.field svg { font-size: 17px; }
.field input {
  flex: 1;
  min-width: 0;
  height: 100%;
  border: 0;
  outline: 0;
  background: none;
  color: var(--label);
  font-size: 16px;
}
.field input::placeholder { color: var(--label-3); }
.field input::-webkit-search-cancel-button { display: none; }
.field kbd {
  font: 12px/1 ui-monospace, "SF Mono", Menlo, monospace;
  padding: 3px 6px;
  border-radius: 5px;
  border: 1px solid var(--separator);
  color: var(--label-3);
}
.field:focus-within kbd { display: none; }

.tokens { display: flex; flex-wrap: wrap; gap: 8px; padding: 0 2px; }
.tokens button {
  min-height: 32px;
  padding: 0 14px;
  border: 0;
  border-radius: 999px;
  background: var(--fill);
  color: var(--label);
  font-size: 14px;
  transition: background var(--fast) var(--ease), transform var(--snappy) var(--spring);
}
.tokens button:hover { background: var(--fill-2); }
.tokens button:active { transform: scale(0.96); }
.tokens button[aria-pressed="true"] { background: var(--accent); color: #fff; }

.group { display: grid; gap: 4px; }
.group h2 {
  display: flex;
  align-items: baseline;
  gap: 6px;
  margin: 0 8px 4px;
  font-size: 13px;
  font-weight: 600;
  color: var(--label-2);
}
.count { font-weight: 400; color: var(--label-3); font-variant-numeric: tabular-nums; }
.group-note { margin: 0 8px; font-size: 13px; color: var(--label-2); }
.group-note.error { color: var(--red); }

.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }

.row {
  display: grid;
  grid-template-columns: 32px 1fr auto;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 52px;
  padding: 8px 10px;
  border: 0;
  border-radius: 10px;
  background: none;
  text-align: left;
  transition: background var(--fast) var(--ease), transform var(--snappy) var(--spring);
}
.row:hover { background: var(--fill); }
.row:active { transform: scale(0.985); }
.row[aria-current="true"] { background: var(--accent-soft); }
.row[aria-current="true"] .glyph { background: var(--accent); color: #fff; }

.glyph {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: var(--fill);
  color: var(--label-2);
  font-size: 18px;
}
.text { min-width: 0; }
.title, .sub {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.title { font-size: 15px; font-weight: 500; }
.sub { font-size: 13px; color: var(--label-2); font-variant-numeric: tabular-nums; }
.sub.error { color: var(--red); white-space: normal; }

.trail {
  display: grid;
  place-items: center;
  min-width: 28px;
  color: var(--label-3);
  font-size: 18px;
}
.row:hover .trail { color: var(--accent); }
.trail.done { color: var(--green); }
.trail.failed { color: var(--red); }

.ring { width: 24px; height: 24px; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 3; }
.ring .track { stroke: var(--fill-2); }
.ring .value { stroke: var(--accent); stroke-linecap: round; stroke-dasharray: 62.83; stroke-dashoffset: 62.83; transition: stroke-dashoffset 120ms linear; }
.ring.spin { animation: spin 0.9s linear infinite; }
.ring.spin .value { stroke-dashoffset: 47; }
@keyframes spin { to { transform: rotate(270deg); } }

.skeleton { height: 52px; border-radius: 10px; background: var(--fill); animation: pulse 1.2s var(--ease) infinite alternate; }
@keyframes pulse { from { opacity: 0.55; } to { opacity: 1; } }

.row.enter { animation: enter var(--snappy) var(--spring) both; }
@keyframes enter { from { opacity: 0; transform: translateY(-6px); } }

.storage { margin-top: auto; padding: 16px 8px 0; border-top: 1px solid var(--separator); }
.storage p { margin: 8px 0 0; font-size: 12px; color: var(--label-2); font-variant-numeric: tabular-nums; }
.meter { height: 4px; border-radius: 99px; background: var(--fill-2); overflow: hidden; }
.meter span { display: block; height: 100%; width: 0; border-radius: inherit; background: var(--label-3); transition: width var(--snappy) var(--ease); }
.meter.high span { background: var(--red); }

.reader {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  padding: 16px 24px 24px;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 56px;
  padding-bottom: 12px;
}
.toolbar-title { flex: 1; min-width: 0; }
.toolbar-title h2 {
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.toolbar-title p {
  margin: 0;
  font-size: 13px;
  color: var(--label-2);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-width: 44px;
  height: 44px;
  padding: 0 10px;
  border: 0;
  border-radius: 10px;
  background: none;
  color: var(--accent);
  font-size: 20px;
  text-decoration: none;
  transition: background var(--fast) var(--ease);
}
.icon:hover { background: var(--fill); }
.icon.danger { color: var(--red); }
.back { display: none; font-size: 17px; padding-left: 4px; }
.back svg { font-size: 22px; }

.page {
  flex: 1;
  display: flex;
  min-height: 0;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  overflow: hidden;
  animation: enter var(--snappy) var(--spring) both;
}
.page iframe { flex: 1; width: 100%; border: 0; background: #fff; }
.page pre {
  flex: 1;
  margin: 0;
  padding: 24px;
  overflow: auto;
  white-space: pre-wrap;
  font: 14px/1.6 ui-monospace, "SF Mono", Menlo, monospace;
}

.unavailable {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 32px;
  text-align: center;
  color: var(--label-2);
}
.unavailable svg { font-size: 56px; color: var(--label-3); margin-bottom: 8px; }
.unavailable h2 { margin: 0; font-size: 22px; font-weight: 700; letter-spacing: -0.02em; color: var(--label); }
.unavailable p { margin: 0; max-width: 360px; }

.tinted {
  margin-top: 16px;
  min-height: 44px;
  padding: 0 20px;
  border: 0;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
  transition: transform var(--snappy) var(--spring), background var(--fast) var(--ease);
}
.tinted:hover { background: color-mix(in srgb, var(--accent) 20%, transparent); }
.tinted:active { transform: scale(0.96); }

.hud {
  position: fixed;
  left: 50%;
  bottom: max(24px, env(safe-area-inset-bottom));
  display: flex;
  align-items: center;
  gap: 16px;
  max-width: calc(100vw - 32px);
  padding: 10px 10px 10px 20px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--sidebar-solid) 80%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  box-shadow: 0 0 0 1px var(--separator), 0 12px 32px rgba(0, 0, 0, 0.16);
  font-size: 14px;
  transform: translateX(-50%);
  animation: hud var(--snappy) var(--spring) both;
}
.hud:not(:has(button:not([hidden]))) { padding-right: 20px; }
.hud button {
  min-height: 32px;
  padding: 0 14px;
  border: 0;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}
@keyframes hud { from { opacity: 0; transform: translate(-50%, 12px) scale(0.96); } }

@media (prefers-reduced-transparency: reduce) {
  .hud { background: var(--sidebar-solid); backdrop-filter: none; -webkit-backdrop-filter: none; }
}

@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { border-right: 0; background: var(--bg); backdrop-filter: none; -webkit-backdrop-filter: none; }
  .reader { display: none; padding: 0 12px max(12px, env(safe-area-inset-bottom)); }
  .app.reading .sidebar { display: none; }
  .app.reading .reader { display: flex; }
  .back { display: inline-flex; }
  .toolbar { position: sticky; top: 0; padding-top: env(safe-area-inset-top); }
  .page { border-radius: 10px; }
}

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