:root{
  --bg: #f5fbf7;
  --panel: #ffffff;
  --ink: #1f2a37;
  --muted: #6b7280;
  --line: #e2ede6;
  --green: #48a876;
  --green-dark: #237851;
  --green-soft: #eaf7f0;
  --purple: #7c68ee;
  --purple-soft: #f1effe;
  --yellow: #f59e0b;
  --yellow-soft: #fef9e7;
  --red: #ef4444;
  --red-soft: #fef2f2;
  --blue: #3b82f6;
  --blue-soft: #eff6ff;
  --m3-elevation-1: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
  --m3-elevation-2: 0 4px 14px rgba(40,80,60,0.08);
  --m3-elevation-3: 0 10px 28px rgba(40,80,60,0.11);
  --radius-sm: 12px;
  --radius-md: 16px;
  --radius-lg: 24px;
  --radius-full: 9999px;
  --touch-target: 48px;
}

*{box-sizing:border-box}

body{
  margin:0;
  font-family:Inter, Roboto, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color:var(--ink);
  background:
    radial-gradient(circle at 12% 8%, #e8f8ee 0 12%, transparent 35%),
    radial-gradient(circle at 92% 5%, #f2efff 0 10%, transparent 30%),
    var(--bg);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

button, input {
  font: inherit;
  outline: none;
}
button {
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

.app {
  max-width: 1180px;
  margin: auto;
  padding: 16px;
  padding-bottom: max(24px, env(safe-area-inset-bottom));
}

/* Material Header */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: rgba(255,255,255,0.92);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 12px 18px;
  box-shadow: var(--m3-elevation-1);
  position: sticky;
  top: 10px;
  z-index: 50;
}
.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}
.logo {
  width: 44px;
  height: 44px;
  border-radius: 14px;
  background: linear-gradient(135deg, #5ec38d, #3c9d6d);
  display: grid;
  place-items: center;
  color: #fff;
  font-weight: 900;
  font-size: 22px;
  box-shadow: 0 4px 12px rgba(60,157,109,0.28);
}
.brand h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 900;
  letter-spacing: -0.02em;
}
.brand p {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 12px;
}
.header-badges {
  display: flex;
  gap: 8px;
  align-items: center;
}
.streak {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--yellow-soft);
  border: 1px solid #fde68a;
  border-radius: var(--radius-full);
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 800;
  color: #92400e;
}
.badge {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius-full);
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 800;
  color: var(--ink);
  box-shadow: var(--m3-elevation-1);
}

/* Layout */
.layout {
  display: grid;
  grid-template-columns: 310px 1fr;
  gap: 20px;
  margin-top: 18px;
}

/* Sidebar */
.sidebar {
  background: rgba(255,255,255,0.96);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--m3-elevation-1);
  padding: 16px;
  align-self: start;
  position: sticky;
  top: 92px;
}
.profile-card {
  background: linear-gradient(135deg, #f0faf3, #f5f2ff);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  padding: 14px;
  margin-bottom: 14px;
  display: grid;
  grid-template-columns: 48px 1fr;
  gap: 12px;
  align-items: center;
}
.avatar {
  width: 48px;
  height: 48px;
  border-radius: 14px;
  background: #fff;
  border: 1px solid var(--line);
  display: grid;
  place-items: center;
  font-size: 26px;
}
.profile-card b {
  font-size: 13px;
}
.profile-card span {
  font-size: 11px;
  color: var(--muted);
}
.goal {
  margin-top: 8px;
  height: 6px;
  background: #e5ede7;
  border-radius: var(--radius-full);
  overflow: hidden;
}
.goal > div {
  height: 100%;
  background: linear-gradient(90deg, var(--green), #6ee7b7);
  width: 0%;
  transition: width 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.side-title {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  font-weight: 800;
  margin: 4px 4px 10px;
}

/* Material Mode Selector (Segmented Button) */
.mode-segmented {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  background: #eef4f0;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  padding: 4px;
}
.mode-btn {
  border: 0;
  background: transparent;
  border-radius: var(--radius-sm);
  min-height: var(--touch-target);
  padding: 8px 6px;
  font-size: 12px;
  font-weight: 800;
  color: var(--muted);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}
.mode-btn:hover {
  color: var(--ink);
  background: rgba(255,255,255,0.6);
}
.mode-btn.active {
  background: #ffffff;
  color: var(--green-dark);
  box-shadow: 0 2px 8px rgba(35,120,81,0.14);
}

.chapter-list {
  display: grid;
  gap: 8px;
}
.chapter {
  width: 100%;
  text-align: left;
  border: 1px solid transparent;
  background: transparent;
  border-radius: var(--radius-md);
  padding: 8px 10px;
  min-height: var(--touch-target);
  display: grid;
  grid-template-columns: 40px 1fr auto;
  gap: 10px;
  align-items: center;
  color: var(--ink);
  transition: all 0.18s ease;
}
.chapter:hover {
  background: #f4faf6;
}
.chapter:active {
  transform: scale(0.98);
}
.chapter.active {
  background: var(--green-soft);
  border-color: #c4ebd4;
}
.chapter .ico {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  background: #fff;
  display: grid;
  place-items: center;
  font-size: 20px;
  border: 1px solid var(--line);
  box-shadow: var(--m3-elevation-1);
}
.chapter .name {
  font-size: 13px;
  font-weight: 800;
}
.chapter .desc {
  font-size: 10px;
  color: var(--muted);
  margin-top: 1px;
}
.chapter .count {
  font-size: 11px;
  font-weight: 800;
  color: var(--green-dark);
  background: #fff;
  border: 1px solid #ccead8;
  border-radius: var(--radius-full);
  padding: 3px 8px;
}
.side-sep {
  height: 1px;
  background: var(--line);
  margin: 14px 0;
}

/* Sidebar Settings & Romaji Switch */
.setting-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  background: #f8faf9;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  margin-bottom: 12px;
  cursor: pointer;
  user-select: none;
  min-height: 48px;
  transition: all 0.2s ease;
}
.setting-item:hover {
  background: #eef5f1;
  border-color: #cde0d5;
}
.setting-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.setting-title {
  font-size: 13px;
  font-weight: 800;
  color: var(--ink);
}
.setting-desc {
  font-size: 11px;
  color: var(--muted);
}
.setting-checkbox {
  width: 22px;
  height: 22px;
  accent-color: var(--green);
  cursor: pointer;
}

/* Romaji Visibility Rules */
body.hide-romaji .ro,
body.hide-romaji .dict-ro,
body.hide-romaji .dict-ex-ro,
body.hide-romaji #exampleRo,
body.hide-romaji .roman-roma {
  display: none !important;
}
body.hide-romaji .choice .jp {
  font-size: 32px;
}

/* Main Surface Card */
.maincard {
  background: rgba(255,255,255,0.98);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--m3-elevation-2);
  padding: 24px;
  min-height: 540px;
  position: relative;
  overflow: hidden;
}
.maincard:before {
  content: "";
  position: absolute;
  right: -50px;
  top: -50px;
  width: 170px;
  height: 170px;
  border-radius: 50%;
  background: linear-gradient(135deg, #e6f7ec, #f0edff);
  opacity: 0.8;
  pointer-events: none;
}
.topline {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  position: relative;
}
.eyebrow {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--green-dark);
  font-weight: 800;
}
.prompt {
  font-size: 44px;
  line-height: 1.1;
  font-weight: 900;
  margin: 14px 0 8px;
  letter-spacing: -0.03em;
  position: relative;
}
.helper {
  color: var(--muted);
  font-size: 14px;
  position: relative;
}
.progress-wrap {
  margin: 18px 0 16px;
}
.progress-meta {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  color: var(--muted);
  margin-bottom: 6px;
  font-weight: 800;
}
.progress {
  height: 8px;
  background: #edf4ef;
  border-radius: var(--radius-full);
  overflow: hidden;
}
.progress > div {
  height: 100%;
  background: linear-gradient(90deg, var(--green), #6ee7b7);
  width: 0%;
  transition: width 0.25s ease;
}

/* Material Inputs & Buttons */
.inputbox {
  display: flex;
  gap: 10px;
  align-items: stretch;
}
input#romaji {
  flex: 1;
  border: 2px solid #d8e5dd;
  border-radius: var(--radius-md);
  padding: 12px 18px;
  font-size: 22px;
  font-weight: 700;
  background: #fff;
  min-height: 52px;
  transition: all 0.2s ease;
}
input#romaji:focus {
  border-color: var(--green);
  box-shadow: 0 0 0 4px rgba(72,168,118,0.15);
}
.input-actions {
  display: flex;
  gap: 8px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: var(--touch-target);
  padding: 12px 20px;
  font-size: 15px;
  font-weight: 800;
  border-radius: var(--radius-md);
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  transition: all 0.18s cubic-bezier(0.4, 0, 0.2, 1);
}
.btn:active {
  transform: scale(0.97);
}
.btn.primary {
  background: linear-gradient(135deg, var(--green), #389665);
  color: #fff;
  border: 0;
  box-shadow: 0 4px 14px rgba(56,150,101,0.28);
}
.btn.primary:hover {
  box-shadow: 0 6px 18px rgba(56,150,101,0.36);
  transform: translateY(-1px);
}
.btn.primary:active {
  transform: scale(0.97);
}
.btn-tonal-danger {
  background: var(--red-soft);
  border: 1px solid #fecaca;
  color: #b91c1c;
}
.btn-tonal-danger:hover {
  background: #fee2e2;
}
.btn-tonal-secondary {
  background: var(--purple-soft);
  border: 1px solid #ddd6fe;
  color: #6d28d9;
}
.btn-tonal-secondary:hover {
  background: #ede9fe;
}
.btn-tonal-secondary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

.kana-preview {
  min-height: 40px;
  font-size: 28px;
  font-weight: 800;
  padding: 8px 4px;
  color: var(--green-dark);
}

.choices {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.choice {
  border: 2px solid #e2ebe5;
  border-radius: var(--radius-md);
  background: #fff;
  padding: 16px;
  color: var(--ink)!important;
  min-height: 96px;
  transition: all 0.18s ease;
  box-shadow: var(--m3-elevation-1);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.choice:hover {
  border-color: #a7dfbc;
  background: #f7fdf9;
  transform: translateY(-1px);
}
.choice:active {
  transform: scale(0.98);
}
.choice .jp {
  font-size: 28px;
  font-weight: 900;
}
.choice .ro {
  margin-top: 4px;
  color: var(--muted)!important;
  font-size: 14px;
  font-weight: 700;
}

.feedback {
  display: none;
  margin-top: 18px;
  border-radius: var(--radius-md);
  padding: 16px 18px;
  border: 1px solid transparent;
}
.feedback.ok {
  display: block;
  background: var(--green-soft);
  border-color: #bbf0cf;
}
.feedback.bad {
  display: block;
  background: var(--red-soft);
  border-color: #fecaca;
}
.feedback .status {
  font-weight: 900;
  margin-bottom: 6px;
  font-size: 14px;
}
.feedback .answer {
  font-size: 28px;
  font-weight: 900;
}
.feedback .roman {
  color: var(--muted);
  margin-top: 2px;
  font-size: 14px;
}

.example {
  display: none;
  margin-top: 14px;
  padding: 16px;
  border-radius: var(--radius-md);
  background: var(--purple-soft);
  border: 1px solid #ddd6fe;
}
.example .tag {
  font-size: 10px;
  font-weight: 900;
  color: #6d28d9;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.example .ja {
  font-size: 21px;
  font-weight: 900;
  margin-top: 6px;
}
.example .ro, .example .it {
  font-size: 14px;
  color: var(--muted);
  margin-top: 3px;
}

/* AI Examples Section */
.ai-examples {
  margin-top: 12px;
  padding: 16px;
  border-radius: var(--radius-md);
  background: linear-gradient(135deg, #fffcf5, #f8f5ff);
  border: 1px solid #e9dff8;
}
.ai-ex-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 11px;
  font-weight: 900;
  color: #5b21b6;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 8px;
}
.ai-example-item {
  padding: 10px 0;
  border-bottom: 1px solid rgba(130, 110, 180, 0.12);
}
.ai-example-item:last-child {
  border-bottom: 0;
  padding-bottom: 0;
}
.ai-example-jp {
  font-size: 19px;
  font-weight: 800;
  line-height: 1.4;
}
.ai-example-it {
  font-size: 13px;
  color: var(--muted);
  margin-top: 3px;
}

/* AI Actions Grid */
.ai-actions-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
@media (max-width: 600px) {
  .ai-actions-row {
    grid-template-columns: 1fr;
  }
}

/* AI Explanation Box */
.ai-explain-box {
  margin-top: 12px;
  padding: 16px;
  border-radius: var(--radius-md);
  background: #fdfcff;
  border: 1px solid #ddd6fe;
  box-shadow: var(--m3-elevation-1);
}
.ai-explain-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px dashed #e4dcf8;
  font-size: 12px;
  font-weight: 800;
  color: #6d28d9;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.ai-explain-section {
  margin-bottom: 12px;
}
.ai-explain-section:last-child {
  margin-bottom: 0;
}
.ai-explain-lbl {
  font-size: 11px;
  font-weight: 800;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 3px;
}
.ai-explain-ja {
  font-size: 19px;
  font-weight: 800;
  color: var(--ink);
  line-height: 1.4;
}
.ai-explain-nuance, .ai-explain-culture {
  font-size: 14px;
  color: var(--ink);
  line-height: 1.45;
}
.ai-loading {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--muted);
  font-size: 13px;
  padding: 8px 0;
}
.ai-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid #ddd6fe;
  border-top-color: var(--purple);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

.hint-row {
  display: flex;
  justify-content: flex-start;
  margin-top: 14px;
}
.hint-btn {
  border: 1px solid #fde68a;
  background: var(--yellow-soft);
  color: #92400e;
  border-radius: var(--radius-md);
  padding: 10px 16px;
  font-weight: 800;
  font-size: 13px;
  min-height: var(--touch-target);
  box-shadow: var(--m3-elevation-1);
}
.hint-btn:hover {
  background: #fef3c7;
}
.hint-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.llm-hint {
  margin-top: 10px;
  padding: 14px 16px;
  border-radius: var(--radius-md);
  background: #fffbeb;
  border: 1px solid #fde68a;
  font-size: 14px;
  line-height: 1.5;
}
.llm-hint .hint-title {
  font-size: 10px;
  font-weight: 900;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #b45309;
  margin-bottom: 4px;
}

.nextrow {
  display: none;
  margin-top: 18px;
  justify-content: flex-end;
}

/* Stats */
.stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  margin-top: 18px;
}
.stat {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  padding: 14px;
  box-shadow: var(--m3-elevation-1);
}
.stat:nth-child(1) { background: var(--blue-soft); }
.stat:nth-child(2) { background: var(--green-soft); }
.stat:nth-child(3) { background: var(--yellow-soft); }
.stat:nth-child(4) { background: var(--purple-soft); }
.stat span {
  display: block;
  color: var(--muted);
  font-size: 10px;
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.stat b {
  font-size: 22px;
  margin-top: 3px;
  display: block;
  font-weight: 900;
}

/* Dictionary View */
.dict-card {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  padding: 14px 16px;
  box-shadow: var(--m3-elevation-1);
  transition: all 0.18s ease;
}
.dict-card:hover {
  border-color: #a7dfbc;
  box-shadow: var(--m3-elevation-2);
}
.dict-head {
  display: flex;
  align-items: baseline;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}
.dict-jp {
  font-size: 22px;
  font-weight: 900;
  color: var(--green-dark);
}
.dict-ro {
  font-size: 14px;
  color: var(--muted);
  font-weight: 700;
}
.dict-it {
  font-size: 16px;
  font-weight: 800;
  color: var(--ink);
  margin-left: 2px;
}
.dict-cat {
  margin-left: auto;
  font-size: 10px;
  font-weight: 800;
  text-transform: uppercase;
  background: var(--green-soft);
  color: var(--green-dark);
  padding: 3px 8px;
  border-radius: var(--radius-full);
}
.dict-ex {
  background: var(--purple-soft);
  border: 1px solid #ddd6fe;
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  margin-top: 6px;
}
.dict-ex-label {
  font-size: 10px;
  font-weight: 900;
  color: #6d28d9;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  margin-bottom: 4px;
}
.dict-ex-ja {
  font-size: 17px;
  font-weight: 800;
  color: var(--ink);
}
.dict-ex-ro, .dict-ex-it {
  font-size: 13px;
  color: var(--muted);
  margin-top: 2px;
}

.empty {
  display: none;
  text-align: center;
  padding: 60px 20px;
}
.empty .big {
  font-size: 56px;
}
.empty h2 {
  margin: 8px 0;
}
.empty p {
  color: var(--muted);
}
.footer-tools {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 16px;
  gap: 12px;
  flex-wrap: wrap;
}
.mini {
  font-size: 11px;
  color: var(--muted);
}
.danger {
  color: #b91c1c;
}

/* ================================================= */
/* Mobile / Responsive Optimization                  */
/* ================================================= */
@media(max-width: 860px) {
  .layout {
    display: flex;
    flex-direction: column;
    gap: 16px;
  }
  main {
    order: 1; /* Practice card comes FIRST on mobile! */
  }
  .sidebar {
    order: 2; /* Chapters & stats below practice card */
    position: static;
    top: 0;
  }
  .chapter-list {
    grid-template-columns: 1fr 1fr;
  }
}

@media(max-width: 600px) {
  .app {
    padding: 10px;
  }
  .header {
    border-radius: var(--radius-md);
    padding: 10px 14px;
  }
  .logo {
    width: 38px;
    height: 38px;
    font-size: 19px;
    border-radius: 10px;
  }
  .brand h1 {
    font-size: 17px;
  }
  .brand p {
    display: none;
  }
  .maincard {
    padding: 18px 16px;
    border-radius: var(--radius-md);
    min-height: auto;
  }
  .prompt {
    font-size: 34px;
  }
  .inputbox {
    flex-direction: column;
    gap: 10px;
  }
  input#romaji {
    font-size: 20px;
    padding: 14px 16px;
    min-height: 52px;
    width: 100%;
  }
  .input-actions {
    display: grid;
    grid-template-columns: 1.4fr 1fr;
    gap: 8px;
    width: 100%;
  }
  .input-actions .btn {
    min-height: var(--touch-target);
    width: 100%;
  }
  .choices {
    grid-template-columns: 1fr;
    gap: 10px;
  }
  .choice {
    min-height: 68px;
    padding: 12px;
  }
  .stats {
    grid-template-columns: 1fr 1fr;
  }
  .chapter-list {
    grid-template-columns: 1fr;
  }
  .nextrow button {
    width: 100%;
    min-height: 50px;
  }
}
