* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --bg: #1a1a2e;
  --surface: #16213e;
  --surface2: #0f3460;
  --accent: #e94560;
  --text: #eee;
  --text-dim: #999;
  --chat-user: #0f3460;
  --chat-bot: #1a1a3e;
  --success: #4ade80;
  --warning: #fbbf24;
  --danger: #ef4444;
  --radius: 8px;
}

body {
  font-family: "SF Mono", "Cascadia Code", "Fira Code", monospace;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

#app {
  max-width: 640px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* --- Nav --- */
nav {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--surface2);
  gap: 16px;
}

.nav-brand {
  font-weight: bold;
  font-size: 1.1em;
  color: var(--accent);
}

.nav-links {
  display: flex;
  gap: 12px;
  flex: 1;
}

.nav-link {
  color: var(--text-dim);
  text-decoration: none;
  font-size: 0.85em;
  padding: 4px 8px;
  border-radius: var(--radius);
  transition: background 0.2s;
}

.nav-link:hover,
.nav-link.active {
  color: var(--text);
  background: var(--surface);
}

.nav-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-btn {
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  font-size: 1.2em;
  position: relative;
}

.badge {
  position: absolute;
  top: -4px;
  right: -6px;
  background: var(--accent);
  color: white;
  font-size: 0.65em;
  padding: 1px 5px;
  border-radius: 10px;
}

#cat-status-mini {
  font-size: 1.3em;
}

/* --- Pages --- */
main {
  flex: 1;
  overflow: hidden;
  display: flex;
}

.page {
  display: none;
  flex-direction: column;
  flex: 1;
  overflow-y: auto;
  padding: 16px;
}

.page.active {
  display: flex;
}

/* --- Buttons --- */
.btn {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--surface2);
  padding: 8px 16px;
  border-radius: var(--radius);
  cursor: pointer;
  font-family: inherit;
  font-size: 0.85em;
  transition: background 0.2s;
}

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

.btn-danger {
  border-color: var(--danger);
  color: var(--danger);
}

.btn-danger:hover {
  background: var(--danger);
  color: white;
}

.btn-small {
  padding: 0.25rem 0.5rem;
  font-size: 0.75rem;
}

.btn-groom {
  background: var(--success);
  color: #111;
  border-color: var(--success);
  font-weight: bold;
}

.btn-groom:hover {
  opacity: 0.9;
}

/* --- Chat --- */
#cat-area {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 0;
  border-bottom: 1px solid var(--surface);
}

#cat-display {
  font-size: 2.5em;
  line-height: 1;
}

#chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.message {
  padding: 10px 14px;
  border-radius: var(--radius);
  max-width: 85%;
  line-height: 1.5;
  font-size: 0.9em;
  word-wrap: break-word;
}

.message em {
  color: var(--text-dim);
  font-style: italic;
}

.message-user {
  background: var(--chat-user);
  align-self: flex-end;
  border-bottom-right-radius: 2px;
}

.message-assistant {
  background: var(--chat-bot);
  align-self: flex-start;
  border-bottom-left-radius: 2px;
}

.message-tool {
  background: var(--surface);
  align-self: flex-start;
  font-size: 0.8em;
  color: var(--text-dim);
  border-left: 3px solid var(--accent);
}

.message-streaming {
  opacity: 0.9;
}

.message-streaming::after {
  content: "\25AE";
  animation: blink 0.8s infinite;
}

@keyframes blink {
  50% { opacity: 0; }
}

#chat-form {
  display: flex;
  gap: 8px;
  padding: 12px 0;
  border-top: 1px solid var(--surface);
}

#chat-input {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--surface2);
  color: var(--text);
  padding: 10px 14px;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 0.9em;
  outline: none;
}

#chat-input:focus {
  border-color: var(--accent);
}

/* --- Flashcards --- */

#flashcard-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 24px;
}

.flashcard {
  width: 100%;
  max-width: 400px;
  height: 220px;
  perspective: 800px;
  cursor: pointer;
}

.flashcard-inner {
  width: 100%;
  height: 100%;
  position: relative;
  transition: transform 0.5s;
  transform-style: preserve-3d;
}

.flashcard.flipped .flashcard-inner {
  transform: rotateY(180deg);
}

.flashcard-front,
.flashcard-back {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  border-radius: var(--radius);
  backface-visibility: hidden;
  font-size: 1.3em;
  text-align: center;
  line-height: 1.4;
}

.flashcard-front {
  background: var(--surface2);
  flex-direction: column;
  gap: 12px;
  overflow: hidden;
}

/* Dog-ear fold in bottom-right corner — hints card is flippable */
.flashcard-front::after {
  content: '';
  position: absolute;
  bottom: 0;
  right: 0;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 0 0 28px 28px;
  border-color: transparent transparent var(--bg) transparent;
  animation: dogear-pulse 2s ease-in-out infinite;
  transform-origin: bottom right;
}

@keyframes dogear-pulse {
  0%, 100% { border-width: 0 0 28px 28px; opacity: 0.7; }
  50%       { border-width: 0 0 38px 38px; opacity: 1; }
}

/* Hide dog-ear once flipped */
.flashcard.flipped .flashcard-front::after {
  display: none;
}

.card-word {
  font-size: 1em;
}

.card-context {
  font-size: 0.52em;
  color: var(--text-dim);
  font-style: italic;
  line-height: 1.5;
}

.flashcard-back {
  background: var(--surface);
  transform: rotateY(180deg);
}

#rating-buttons {
  display: flex;
  gap: 8px;
}

.btn-rating {
  min-width: 60px;
}

.btn-rating[data-rating="1"] { border-color: var(--danger); color: var(--danger); }
.btn-rating[data-rating="2"] { border-color: var(--warning); color: var(--warning); }
.btn-rating[data-rating="3"] { border-color: var(--success); color: var(--success); }
.btn-rating[data-rating="4"] { border-color: #60a5fa; color: #60a5fa; }

.btn-rating:hover { color: white !important; }
.btn-rating[data-rating="1"]:hover { background: var(--danger); }
.btn-rating[data-rating="2"]:hover { background: var(--warning); }
.btn-rating[data-rating="3"]:hover { background: var(--success); }
.btn-rating[data-rating="4"]:hover { background: #60a5fa; }

#card-actions {
  gap: 8px;
  margin-top: -8px;
}

#card-actions:not([hidden]) {
  display: flex;
}

/* --- Edit card form --- */
.edit-card-form {
  width: 100%;
  max-width: 400px;
  flex-direction: column;
  gap: 8px;
}

.edit-card-form:not([hidden]) {
  display: flex;
}

.edit-card-form label {
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 0.8em;
  color: var(--text-dim);
}

.edit-card-form input {
  background: var(--surface);
  border: 1px solid var(--surface2);
  color: var(--text);
  padding: 6px 10px;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 1em;
  outline: none;
}

.edit-card-form input:focus {
  border-color: var(--accent);
}

.edit-card-actions {
  display: flex;
  gap: 8px;
}

/* --- Review stats bar --- */
#review-stats {
  padding: 8px 0;
  font-size: 0.85em;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  gap: 12px;
}

#delete-all-btn { margin-left: auto; }

/* --- Goals --- */
#goals-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 12px;
}

.goal-card {
  background: var(--surface);
  padding: 16px;
  border-radius: var(--radius);
}

.goal-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
}

.goal-title {
  font-weight: bold;
  margin-bottom: 4px;
}

.goal-delete-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 1.2em;
  line-height: 1;
  cursor: pointer;
  padding: 0 2px;
  flex-shrink: 0;
}

.goal-delete-btn:hover {
  color: var(--danger, #e05);
}

.goal-meta {
  font-size: 0.8em;
  color: var(--text-dim);
}

.goal-progress {
  margin-top: 8px;
  height: 6px;
  background: var(--surface2);
  border-radius: 3px;
  overflow: hidden;
}

.goal-progress-bar {
  height: 100%;
  background: var(--success);
  transition: width 0.3s;
}

.add-goal-form {
  display: flex;
  gap: 8px;
  margin-top: 12px;
}

.add-goal-form input,
.add-goal-form select {
  background: var(--surface);
  border: 1px solid var(--surface2);
  color: var(--text);
  padding: 6px 10px;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 0.85em;
  outline: none;
}

.add-goal-form input {
  flex: 1;
}

.add-goal-form input:focus,
.add-goal-form select:focus {
  border-color: var(--accent);
}

.goal-actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.btn-action {
  background: var(--surface2);
  border-color: var(--accent);
  color: var(--accent);
}

.btn-action:hover {
  background: var(--accent);
  color: white;
}

.import-form {
  display: flex;
  gap: 8px;
  margin-top: 8px;
  align-items: center;
}

.import-form input {
  background: var(--surface2);
  border: 1px solid var(--surface2);
  color: var(--text);
  padding: 4px 8px;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 0.8em;
  width: 80px;
  outline: none;
}

.import-form input:focus {
  border-color: var(--accent);
}

.goal-import-result {
  margin-top: 8px;
  font-size: 0.8em;
}

.goal-import-success {
  color: var(--success);
}

.goal-import-error {
  color: var(--danger);
}

/* --- Settings --- */
#settings-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-top: 12px;
}

#settings-form label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.85em;
  color: var(--text-dim);
}

#settings-form input {
  background: var(--surface);
  border: 1px solid var(--surface2);
  color: var(--text);
  padding: 8px 12px;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 1em;
  outline: none;
}

#settings-form input:focus {
  border-color: var(--accent);
}

#settings-status {
  font-size: 0.85em;
  color: var(--success);
}

hr {
  border: none;
  border-top: 1px solid var(--surface);
  margin: 16px 0;
}

/* --- Notifications --- */
.notif-panel {
  position: absolute;
  top: 50px;
  right: 16px;
  width: 300px;
  max-height: 400px;
  background: var(--surface);
  border: 1px solid var(--surface2);
  border-radius: var(--radius);
  overflow-y: auto;
  z-index: 100;
}

.notif-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  border-bottom: 1px solid var(--surface2);
  font-size: 0.85em;
}

.notif-header button {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  font-family: inherit;
  font-size: 0.85em;
}

.notif-item {
  padding: 10px 14px;
  border-bottom: 1px solid var(--surface2);
  font-size: 0.85em;
}

.notif-item.unread {
  border-left: 3px solid var(--accent);
}

.notif-item-title {
  font-weight: bold;
}

.notif-item-body {
  color: var(--text-dim);
  margin-top: 2px;
}

/* --- Answer input area --- */
#answer-area {
  width: 100%;
  max-width: 400px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.answer-input-row {
  display: flex;
  gap: 8px;
}

#answer-input {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--surface2);
  color: var(--text);
  padding: 8px 12px;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 0.9em;
  outline: none;
}

#answer-input:focus {
  border-color: var(--accent);
}

#recording-status {
  font-size: 0.8em;
  color: var(--danger);
  display: flex;
  align-items: center;
  gap: 6px;
}

.recording-dot {
  animation: blink 0.8s infinite;
}

/* --- Pre-flip actions --- */
#preflip-actions {
  display: flex;
  gap: 8px;
}

/* --- Evaluation panel --- */
#eval-panel {
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius);
  padding: 10px 14px;
  font-size: 0.85em;
  line-height: 1.5;
}

#eval-explanation {
  color: var(--text);
}

/* --- Rating button states --- */
.btn-rating-suggested {
  outline: 2px solid currentColor;
  outline-offset: 2px;
  font-weight: bold;
}

.btn-rating-disabled {
  opacity: 0.35;
  cursor: not-allowed !important;
}

/* --- Grammar card back --- */
.card-type-chip {
  font-size: 0.6em;
  background: var(--surface2);
  color: var(--text-dim);
  padding: 2px 8px;
  border-radius: 10px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.flashcard-back {
  flex-direction: column;
  gap: 10px;
  padding: 16px 20px;
  font-size: 1em;
  overflow-y: auto;
}

.conjugation-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9em;
}

.conjugation-table td {
  padding: 4px 8px;
}

.conjugation-table .person {
  color: var(--text-dim);
  text-align: right;
  width: 45%;
}

.conjugation-table .form {
  color: var(--text);
  font-weight: bold;
}

.grammar-answer {
  font-size: 1.2em;
  font-weight: bold;
  color: var(--success);
}

.grammar-rule {
  font-size: 0.75em;
  color: var(--text-dim);
  font-style: italic;
}

.grammar-example {
  font-size: 0.8em;
  color: var(--text-dim);
  border-left: 2px solid var(--accent);
  padding-left: 8px;
}

/* --- Responsive --- */
@media (max-width: 480px) {
  nav {
    padding: 8px 12px;
    gap: 8px;
  }

  .nav-links {
    gap: 4px;
  }

  .nav-link {
    font-size: 0.75em;
    padding: 4px 6px;
  }

  .message {
    max-width: 92%;
  }
}
