:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #172026;
  --muted: #64717d;
  --line: #dbe1e7;
  --primary: #2563eb;
  --primary-text: #ffffff;
  --danger: #b91c1c;
  --warn: #b45309;
  --ok: #047857;
  --easy: #1d4ed8;
}

body.dark {
  color-scheme: dark;
  --bg: #101418;
  --panel: #171d23;
  --text: #eef3f7;
  --muted: #9aa8b5;
  --line: #2a3440;
  --primary: #60a5fa;
  --primary-text: #07111f;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  line-height: 1.6;
}

.app-shell {
  width: min(960px, 100%);
  margin: 0 auto;
  padding: 16px 16px 96px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
}

.quick-nav {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  margin: -4px 0 14px;
  padding-bottom: 4px;
}

.quick-nav a {
  flex: 0 0 auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  color: var(--text);
  text-decoration: none;
  background: var(--panel);
  font-size: 0.9rem;
}

h1, h2, p {
  margin: 0;
}

h1 {
  font-size: 1.45rem;
}

h2 {
  font-size: 1.1rem;
  margin-bottom: 12px;
}

#sync-status,
.phase-label,
.study-meta,
.message {
  color: var(--muted);
  font-size: 0.92rem;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 14px;
}

label {
  display: grid;
  gap: 6px;
  margin-bottom: 12px;
  color: var(--muted);
}

input,
textarea,
select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}

button {
  border: 0;
  border-radius: 8px;
  background: var(--primary);
  color: var(--primary-text);
  padding: 12px 14px;
  min-height: 48px;
  font-weight: 700;
  font: inherit;
  touch-action: manipulation;
}

button.secondary,
.icon-button,
.segmented {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--line);
}

.segmented.active {
  border-color: var(--primary);
  color: var(--primary);
}

.primary-action {
  font-size: 1.02rem;
}

.actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.stack {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

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

.list-toolbar {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 12px;
}

.form-grid label {
  margin-bottom: 0;
}

.wide {
  grid-column: 1 / -1;
}

.mode-switch {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 12px 0;
}

.low-energy-note {
  border: 1px solid var(--line);
  border-left: 4px solid var(--primary);
  border-radius: 8px;
  padding: 12px;
  background: var(--bg);
  color: var(--text);
  font-weight: 700;
}

.low-energy-note.limit-reached {
  border-left-color: var(--ok);
}

.study-meta {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.card-face {
  display: grid;
  gap: 12px;
}

.question,
.answer {
  white-space: pre-wrap;
  font-size: 1.08rem;
  overflow-wrap: anywhere;
}

.answer {
  border-top: 1px solid var(--line);
  padding-top: 12px;
  background: color-mix(in srgb, var(--primary) 6%, transparent);
  border-radius: 8px;
  padding: 12px;
}

.session-result {
  margin-top: 16px;
  border-top: 1px solid var(--line);
  padding-top: 14px;
}

.session-result h3 {
  margin: 0 0 10px;
  font-size: 1rem;
}

.code {
  overflow-x: auto;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  padding: 12px;
  border-radius: 8px;
  background: #0f172a;
  color: #e2e8f0;
  max-height: 45vh;
}

.confidence {
  grid-template-columns: auto 1fr auto;
  align-items: center;
  margin-top: 16px;
}

.rating-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  margin-top: 12px;
}

.rating-grid button {
  min-height: 56px;
}

.danger { background: var(--danger); color: #fff; }
.warn { background: var(--warn); color: #fff; }
.ok { background: var(--ok); color: #fff; }
.easy { background: var(--easy); color: #fff; }

.fab {
  position: fixed;
  right: 18px;
  bottom: 18px;
  min-width: 144px;
  box-shadow: 0 12px 26px rgba(0, 0, 0, .24);
  z-index: 10;
}

.hidden {
  display: none !important;
}

.message {
  border-left: 4px solid var(--primary);
  white-space: pre-wrap;
}

.inline-check {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
}

.inline-check input {
  width: auto;
}

.card-list {
  display: grid;
  gap: 10px;
  margin-top: 14px;
}

.card-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: var(--bg);
}

.row-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.button-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
}

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

.detail-item {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  background: var(--bg);
}

.detail-item span {
  display: block;
  color: var(--muted);
  font-size: 0.82rem;
}

.detail-item strong {
  display: block;
  overflow-wrap: anywhere;
}

.detail-history {
  grid-column: 1 / -1;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: var(--bg);
}

.detail-history h3 {
  margin: 0 0 8px;
  font-size: 1rem;
}

.section-subtitle {
  margin: 16px 0 0;
  font-size: 1rem;
}

.mini-history-row {
  border-top: 1px solid var(--line);
  padding: 8px 0;
  color: var(--muted);
}

.text-button {
  display: inline;
  min-height: 0;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--text);
  text-align: left;
  font-weight: 700;
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  margin-top: 12px;
}

.stat-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: var(--bg);
}

.stat-card span {
  display: block;
  color: var(--muted);
  font-size: 0.86rem;
}

.stat-card strong {
  display: block;
  font-size: 1.35rem;
}

.empty-state {
  border: 1px dashed var(--line);
  border-radius: 8px;
  padding: 14px;
  color: var(--muted);
}

.prompt-list {
  display: grid;
  gap: 12px;
  margin-top: 14px;
}

.prompt-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: var(--bg);
}

.prompt-header {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: start;
}

.prompt-header h3 {
  margin: 0 0 4px;
  font-size: 1rem;
}

.prompt-text {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  overflow: auto;
  max-height: 260px;
  margin: 12px 0 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: var(--panel);
  color: var(--text);
  font: 0.9rem/1.55 ui-monospace, SFMono-Regular, Consolas, monospace;
}

.result-box,
.preview-list {
  margin-top: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}

.result-row,
.preview-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  padding: 12px;
  border-bottom: 1px solid var(--line);
}

.result-row:last-child,
.preview-row:last-child {
  border-bottom: 0;
}

.row-title {
  font-weight: 700;
}

.row-detail {
  color: var(--muted);
  font-size: 0.9rem;
}

.badge {
  align-self: start;
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 0.8rem;
  border: 1px solid var(--line);
}

.badge.ok {
  background: transparent;
  color: var(--ok);
}

.badge.ng {
  color: var(--danger);
}

.badge.warn {
  background: transparent;
  color: var(--warn);
}

@media (max-width: 640px) {
  .stack,
  .rating-grid,
  .form-grid,
  .list-toolbar,
  .detail-grid,
  .stat-grid {
    grid-template-columns: 1fr 1fr;
  }

  .form-grid {
    grid-template-columns: 1fr;
  }

  .card-row {
    grid-template-columns: 1fr;
  }

  .row-actions {
    justify-content: stretch;
  }

  .row-actions > * {
    flex: 1;
  }

  .prompt-header {
    grid-template-columns: 1fr;
  }

  .app-shell {
    padding-inline: 12px;
  }

  .study-panel {
    padding-bottom: 88px;
  }

  .study-meta {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .study-meta a {
    grid-column: 1 / -1;
  }

  .fab {
    left: 12px;
    right: 12px;
    bottom: 12px;
    width: auto;
  }
}

@media (max-width: 420px) {
  .list-toolbar,
  .detail-grid,
  .stat-grid,
  .stack,
  .rating-grid {
    grid-template-columns: 1fr;
  }
}
