:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --text: #1f2933;
  --muted: #6b7280;
  --primary: #2563eb;
  --ok: #16a34a;
  --bad: #dc2626;
  --border: #e5e7eb;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111418;
    --card: #1b1f24;
    --text: #e5e7eb;
    --muted: #9ca3af;
    --primary: #60a5fa;
    --ok: #4ade80;
    --bad: #f87171;
    --border: #2d333b;
  }
}

* { box-sizing: border-box; }

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

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  max-width: 820px;
  margin: 0 auto;
}

header h1 { font-size: 1.3rem; margin: 0; }
.streak { color: var(--muted); font-size: 0.95rem; }

nav {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding: 0 16px 8px;
  max-width: 820px;
  margin: 0 auto;
}

nav button {
  flex: 0 0 auto;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 999px;
  cursor: pointer;
  font-size: 0.95rem;
}

nav button.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

main { max-width: 820px; margin: 0 auto; padding: 0 16px 40px; }

.tab { display: none; }
.tab.active { display: block; }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 16px;
}

.big { font-size: 2rem; font-weight: 600; margin: 8px 0; }
.muted { color: var(--muted); }
.center { text-align: center; }

.row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.center .row { justify-content: center; }

button.btn {
  border: none;
  background: var(--primary);
  color: #fff;
  padding: 10px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.95rem;
}

button.btn.secondary {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
}

button.btn.ok { background: var(--ok); }
button.btn.bad { background: var(--bad); }
button.btn:disabled { opacity: 0.5; cursor: default; }

input[type="text"], textarea, select {
  width: 100%;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
}

textarea { min-height: 160px; resize: vertical; }

.word-ok { color: var(--ok); font-weight: 600; }
.word-bad { color: var(--bad); text-decoration: line-through; }
.word-miss { color: var(--bad); font-weight: 600; }

.score { font-size: 1.2rem; font-weight: 600; margin-top: 12px; }

.options label {
  display: block;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  margin: 6px 0;
  cursor: pointer;
}

.options label.correct { border-color: var(--ok); }
.options label.wrong { border-color: var(--bad); }

.passage { white-space: pre-line; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; }
.stat { background: var(--bg); border-radius: 8px; padding: 12px; text-align: center; }
.stat .num { font-size: 1.6rem; font-weight: 600; }

.bar { height: 8px; background: var(--border); border-radius: 4px; overflow: hidden; margin-top: 6px; }
.bar > div { height: 100%; background: var(--primary); }

ul.history { padding-left: 18px; }

/* Tránh chạm 2 lần bị phóng to trên điện thoại */
button, label, select, input { touch-action: manipulation; }

/* Điện thoại */
@media (max-width: 600px) {
  header {
    padding-top: calc(12px + env(safe-area-inset-top));
    padding-left: calc(16px + env(safe-area-inset-left));
    padding-right: calc(16px + env(safe-area-inset-right));
  }

  /* Thanh tab cố định ở đáy màn hình để bấm bằng ngón cái */
  nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 10;
    max-width: none;
    margin: 0;
    gap: 0;
    padding: 4px 4px calc(4px + env(safe-area-inset-bottom));
    background: var(--card);
    border-top: 1px solid var(--border);
    overflow-x: visible;
  }

  nav button {
    flex: 1 1 0;
    min-width: 0;
    min-height: 44px;
    padding: 6px 2px;
    border: none;
    border-radius: 8px;
    font-size: 0.8rem;
  }

  main {
    padding-left: calc(16px + env(safe-area-inset-left));
    padding-right: calc(16px + env(safe-area-inset-right));
    padding-bottom: calc(80px + env(safe-area-inset-bottom));
  }

  .card { padding: 16px; }
  .big { font-size: 1.7rem; word-break: break-word; }
  h2 { font-size: 1.2rem; }

  button.btn { min-height: 44px; flex: 1 1 auto; }
  .options label { padding: 12px 10px; }
  .options input { transform: scale(1.2); margin-right: 6px; }
}

/* Thanh chọn trình độ ở tab Từ vựng */
.toolbar { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; padding: 12px 16px; }
.toolbar select { width: auto; flex: 1 1 160px; }

.badge {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--primary);
  color: #fff;
  font-size: 0.8rem;
  font-weight: 600;
}

button.btn.small { padding: 4px 10px; min-height: 0; flex: 0 0 auto; }

/* Kế hoạch hôm nay */
.plan { display: flex; flex-direction: column; gap: 8px; margin: 12px 0; }

.plan-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  text-align: left;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}

.plan-item .plan-text { flex: 1; }

.plan-item .check {
  flex: 0 0 28px;
  height: 28px;
  border-radius: 50%;
  border: 2px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
}

.plan-item.done .check { background: var(--ok); border-color: var(--ok); color: #fff; }
.plan-item.done .plan-text b { text-decoration: line-through; color: var(--muted); }

h3 { font-size: 1rem; margin: 16px 0 4px; }
