:root {
  --dark: #171218;
  --dark-soft: #241d24;
  --dark-card: #2b232b;
  --accent: #7f2146;
  --accent-strong: #9e3159;
  --accent-soft: #efb3c9;
  --accent-contrast: #fff7fa;
  --surface: #171218;
  --surface-raised: #241d24;
  --surface-card: #302730;
  --text: #f8edf2;
  --muted: #cbbcc4;
  --border: #483944;
  --danger: #b94a67;
  --success: #69ad86;
  --warning: #d79a55;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
}

body[data-theme="light"] {
  --surface: #f7f2f5;
  --surface-raised: #ffffff;
  --surface-card: #fffafd;
  --text: #251923;
  --muted: #6d5c67;
  --border: #dfd2da;
  --accent-contrast: #ffffff;
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  min-height: 100vh;
  margin: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  color: var(--text);
  font-family: var(--font);
  line-height: 1.5;
  transition: background-color 180ms ease, color 180ms ease;
}

body[data-theme="light"] .app-header { background: rgba(255, 255, 255, 0.94); }
body[data-theme="light"] .study-summary { background: #eee4ea; }
body[data-theme="light"] .deck-card { background: var(--surface-card); }

button, input, textarea, select { font: inherit; }
button, a, input, textarea, select { -webkit-tap-highlight-color: transparent; }
button { cursor: pointer; }

:focus-visible {
  outline: 3px solid var(--accent-soft);
  outline-offset: 3px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  border-bottom: 1px solid var(--border);
  background: rgba(23, 18, 24, 0.96);
  backdrop-filter: blur(12px);
}

.header-inner {
  max-width: 1180px;
  min-height: 72px;
  margin: 0 auto;
  padding: 10px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
  text-decoration: none;
  font-size: 1.15rem;
  font-weight: 800;
}

.brand-mark {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-contrast);
  font-weight: 900;
}

.header-actions { display: flex; align-items: center; gap: 8px; }
.create-wrap { position: relative; }

.button, .icon-button, .close-button, .review-button {
  border: 0;
  border-radius: 12px;
  font-weight: 800;
}

.button {
  min-height: 44px;
  padding: 10px 16px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.button-primary { background: var(--accent); color: var(--accent-contrast); }
.button-primary:hover { background: var(--accent-strong); }
.button-accent { background: var(--accent-soft); color: #351220; }
.button-accent:hover { background: #f5c3d5; }
.button-secondary { background: var(--surface-raised); color: var(--text); border: 1px solid var(--border); }

.icon-button {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  background: var(--surface-raised);
  color: var(--text);
  border: 1px solid var(--border);
}

.icon-button svg, .search-box svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.discord-profile {
  min-height: 44px;
  margin-left: 4px;
  padding: 5px 8px 5px 5px;
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
  text-decoration: none;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface-raised);
}

.avatar {
  width: 34px;
  height: 34px;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  border-radius: 10px;
  background: var(--accent-soft);
  color: #351220;
  font-weight: 900;
  overflow: hidden;
}

.avatar img { width: 100%; height: 100%; object-fit: cover; }
.profile-copy { display: flex; flex-direction: column; line-height: 1.15; }
.profile-copy strong { max-width: 150px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.profile-copy small { margin-top: 3px; color: var(--muted); }

.action-menu {
  position: absolute;
  right: 0;
  top: calc(100% + 8px);
  width: 180px;
  padding: 6px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-raised);
  box-shadow: 0 18px 44px rgba(0, 0, 0, 0.28);
}

.action-menu button {
  width: 100%;
  padding: 11px 12px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  text-align: left;
  font-weight: 750;
}
.action-menu button:hover { background: var(--surface-card); }

.app-main {
  width: 100%;
  max-width: 1180px;
  margin: 0 auto;
  flex: 1;
  padding: 28px 24px 48px;
}

.study-summary {
  min-height: 158px;
  padding: 28px 32px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  border: 1px solid var(--border);
  border-radius: 22px;
  background: var(--accent);
  color: var(--accent-contrast);
}

.study-summary h1 { margin: 3px 0 5px; font-size: clamp(1.65rem, 4vw, 2.35rem); }
.study-summary p { margin: 0; }
.study-summary .eyebrow { color: var(--accent-soft); }
.study-summary .button-accent { flex: 0 0 auto; }

.eyebrow {
  margin: 0 0 4px;
  color: var(--accent-soft);
  font-size: 0.72rem;
  font-weight: 900;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.toolbar {
  margin: 32px 0 18px;
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 20px;
}
.toolbar h2 { margin: 0; font-size: 1.55rem; }

.search-box {
  width: min(280px, 100%);
  height: 44px;
  padding: 0 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-raised);
  color: var(--muted);
}
.search-box input {
  width: 100%;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text);
}

.category-tabs {
  margin-bottom: 22px;
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 4px;
}
.category-tab {
  flex: 0 0 auto;
  min-height: 38px;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-raised);
  color: var(--muted);
  font-weight: 800;
}
.category-tab.active { border-color: var(--accent); background: var(--accent); color: var(--accent-contrast); }

.deck-groups { display: grid; gap: 24px; }
.deck-section { min-width: 0; }
.deck-heading {
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.deck-heading h3 { margin: 0; display: flex; align-items: center; gap: 8px; }
.deck-heading-actions { display: flex; gap: 6px; }
.deck-heading-actions button { min-height: 36px; padding: 6px 10px; }

.deck-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 12px;
}

.deck-card {
  position: relative;
  min-height: 150px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface-card);
  color: var(--text);
  overflow: hidden;
  text-align: left;
}
.deck-card:hover { border-color: var(--accent-soft); transform: translateY(-2px); }
.deck-card:active { transform: translateY(0); }
.deck-card::before {
  content: "";
  position: absolute;
  inset: 0 auto 0 0;
  width: 5px;
  background: var(--category-color, var(--accent));
}
.deck-card-index { color: var(--muted); font-size: 0.75rem; font-weight: 800; }
.deck-card strong { margin: 12px 0; display: -webkit-box; overflow: hidden; -webkit-line-clamp: 4; -webkit-box-orient: vertical; }
.deck-card-meta { color: var(--muted); font-size: 0.78rem; }

.deck-pagination {
  margin-top: 12px;
  display: flex;
  justify-content: center;
  gap: 6px;
}
.deck-pagination button {
  min-width: 38px;
  height: 38px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-raised);
  color: var(--text);
}
.deck-pagination button.active { background: var(--accent); color: var(--accent-contrast); border-color: var(--accent); }

.empty-state {
  padding: 42px 20px;
  border: 1px dashed var(--border);
  border-radius: 18px;
  text-align: center;
  color: var(--muted);
}
.empty-state strong { display: block; margin-bottom: 6px; color: var(--text); }

.toast-region {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 80;
  display: grid;
  gap: 8px;
}
.toast {
  max-width: 340px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-raised);
  color: var(--text);
  font-weight: 750;
}

.modal {
  width: 100%;
  max-height: 100%;
  border: 0;
  padding: 0;
  background: transparent;
  color: var(--text);
}
.modal::backdrop { background: rgba(10, 7, 11, 0.72); backdrop-filter: blur(5px); }

.modal-panel {
  width: min(520px, calc(100vw - 28px));
  max-height: calc(100vh - 28px);
  margin: auto;
  padding: 22px;
  display: grid;
  gap: 16px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--surface-raised);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.38);
}
.modal-panel.wide { width: min(760px, calc(100vw - 28px)); }

.modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}
.modal-header h2 { margin: 0; }

.close-button {
  width: 38px;
  height: 38px;
  flex: 0 0 auto;
  border: 1px solid var(--border);
  background: var(--surface-card);
  color: var(--text);
  font-size: 1.35rem;
  line-height: 1;
}

.modal label {
  display: grid;
  gap: 7px;
  color: var(--text);
  font-size: 0.88rem;
  font-weight: 800;
}
.modal input, .modal textarea, .modal select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  padding: 11px 12px;
  outline: 0;
}
.modal textarea { resize: vertical; }
.modal input:focus, .modal textarea:focus, .modal select:focus { border-color: var(--accent-soft); }

.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }

.import-instructions {
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-card);
  color: var(--muted);
  font-size: 0.84rem;
}
.import-instructions p { margin: 4px 0; }
.import-instructions code { color: var(--text); }
.preview-wrap { border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
.preview-header {
  padding: 10px 12px;
  display: flex;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
  color: var(--muted);
}
.import-preview { max-height: 220px; overflow-y: auto; }
.preview-item {
  padding: 10px 12px;
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  align-items: center;
  gap: 8px;
  border-bottom: 1px solid var(--border);
  font-size: 0.82rem;
}
.preview-item:last-child { border-bottom: 0; }
.preview-item input { padding: 8px; }
.preview-item button { width: 34px; height: 34px; }

.category-create {
  display: grid;
  grid-template-columns: 1fr 110px 150px auto;
  align-items: end;
  gap: 10px;
}
.category-create input[type="color"] { height: 44px; padding: 5px; }
.category-list { display: grid; gap: 8px; }
.category-row {
  min-height: 58px;
  padding: 8px 10px;
  display: grid;
  grid-template-columns: 34px 1fr auto;
  align-items: center;
  gap: 10px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-card);
}
.category-icon {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  color: #fff;
  font-weight: 900;
}
.category-row-actions { display: flex; gap: 6px; }
.category-row-actions button { min-height: 36px; padding: 6px 9px; }

.setting-group { border: 0; padding: 0; margin: 0; }
.setting-group legend { margin-bottom: 8px; font-weight: 850; }
.segmented { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.segmented button {
  min-height: 44px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font-weight: 800;
}
.segmented button.active { border-color: var(--accent); background: var(--accent); color: var(--accent-contrast); }

.palette-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.palette-option {
  min-height: 54px;
  padding: 7px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
}
.palette-option.active { outline: 2px solid var(--accent-soft); }
.palette-option span { border-radius: 7px; }

.card-modal { width: 100vw; height: 100vh; max-width: none; max-height: none; }
.study-panel {
  position: relative;
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-rows: auto minmax(260px, 1fr) auto;
  align-items: center;
  gap: 18px;
  padding: 24px max(24px, calc((100vw - 760px) / 2));
  background: var(--surface);
}
.study-close { position: absolute; right: 20px; top: 20px; z-index: 3; }
.study-progress { width: min(620px, calc(100% - 70px)); display: grid; gap: 8px; }
.progress-track { height: 6px; overflow: hidden; border-radius: 999px; background: var(--border); }
.progress-track div { width: 0; height: 100%; border-radius: 999px; background: var(--accent-soft); transition: width 180ms ease; }

.flashcard {
  width: min(620px, 100%);
  height: min(460px, 52vh);
  min-height: 280px;
  justify-self: center;
  border: 0;
  background: transparent;
  perspective: 1200px;
  color: var(--text);
}
.flashcard-inner {
  position: relative;
  width: 100%;
  height: 100%;
  display: block;
  transform-style: preserve-3d;
  transition: transform 520ms cubic-bezier(.2,.75,.25,1);
}
.flashcard.flipped .flashcard-inner { transform: rotateY(180deg); }
.flashcard-face {
  position: absolute;
  inset: 0;
  padding: 38px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  border: 1px solid var(--border);
  border-radius: 24px;
  background: var(--surface-card);
  backface-visibility: hidden;
  text-align: center;
}
.flashcard-face strong { max-width: 520px; font-size: clamp(1.35rem, 4vw, 2.15rem); }
.flashcard-face small { color: var(--muted); }
.flashcard-back { transform: rotateY(180deg); background: var(--accent); color: var(--accent-contrast); }
.flashcard-back .card-kicker { color: var(--accent-soft); }
.card-kicker { color: var(--accent-soft); font-size: 0.75rem; font-weight: 900; letter-spacing: 0.1em; text-transform: uppercase; }

.review-actions {
  width: min(620px, 100%);
  justify-self: center;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}
.review-button { min-height: 52px; color: #fff; }
.difficult { background: #9c3d58; }
.good { background: var(--accent); }
.easy { background: var(--success); color: #102218; }
.delete-card-button { position: absolute; left: 20px; bottom: 20px; color: var(--danger); }

footer {
  margin-top: auto;
  padding: 18px 20px;
  border-top: 1px solid var(--border);
  background: var(--surface-raised);
  color: var(--muted);
  text-align: center;
  font-size: 0.82rem;
  font-weight: 750;
}
footer code { color: var(--text); }

@media (max-width: 900px) {
  .deck-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .profile-copy { display: none; }
  .discord-profile { padding-right: 5px; }
}

@media (max-width: 680px) {
  .header-inner { min-height: 64px; padding: 8px 12px; }
  .brand > span:last-child { display: none; }
  .brand-mark { width: 36px; height: 36px; }
  .header-actions { gap: 5px; }
  .button-primary#createButton { width: 44px; padding: 10px; font-size: 0; }
  #createButton span { font-size: 1.35rem; }
  .action-menu { right: -100px; }
  .app-main { padding: 18px 12px 36px; }
  .study-summary { min-height: 145px; padding: 22px 18px; align-items: flex-start; flex-direction: column; }
  .study-summary .button { width: 100%; }
  .toolbar { margin-top: 24px; align-items: stretch; flex-direction: column; }
  .search-box { width: 100%; }
  .deck-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .deck-card { min-height: 142px; }
  .category-create { grid-template-columns: 1fr 70px; }
  .category-create label:nth-of-type(3) { grid-column: 1 / -1; }
  .category-create .button { grid-column: 1 / -1; }
  .palette-grid { grid-template-columns: repeat(2, 1fr); }
  .preview-item { grid-template-columns: 1fr auto; }
  .preview-item input:nth-of-type(2) { grid-column: 1 / -1; grid-row: 2; }
  .study-panel { padding: 68px 12px 94px; grid-template-rows: auto minmax(260px, 1fr) auto; }
  .study-progress { width: calc(100% - 52px); }
  .flashcard { height: 100%; min-height: 260px; }
  .flashcard-face { padding: 24px 16px; }
  .review-actions { gap: 6px; }
  .review-button { min-height: 48px; padding: 8px 4px; }
  .delete-card-button { bottom: 28px; }
}

@media (max-width: 420px) {
  .deck-grid { grid-template-columns: 1fr; }
  .deck-card { min-height: 120px; }
  .discord-profile { max-width: 44px; }
  .profile-copy { display: none; }
  .modal-panel { width: calc(100vw - 16px); max-height: calc(100vh - 16px); padding: 16px; }
  .category-row { grid-template-columns: 34px 1fr; }
  .category-row-actions { grid-column: 1 / -1; justify-content: flex-end; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    scroll-behavior: auto;
    transition-duration: 0.01ms;
  }
  .flashcard-inner { transition-duration: 0.01ms; }
}
