/* Cozy, calm, study-friendly theme */
:root {
  --bg: #f7f4ef;          /* warm beige */
  --card: #ffffff;
  --text: #2b2b2b;
  --muted: #6b6b6b;
  --primary: #4b6b8a;     /* muted blue */
  --primary-contrast: #ffffff;
  --accent: #9bb7cf;
  --border: #e7e2da;
  --success: #2e7d32;
  --error: #b00020;
  --tint: 155, 183, 207;  /* rgb tuple used for soft tints */

  /* Highlight defaults */
  --hl-bg: rgba(255, 221, 87, 0.45);
  --hl-fg: inherit;

  /* Motion & radii */
  --radius: 16px;
  --radius-sm: 12px;
  --elev: 0 1px 2px rgba(0,0,0,0.03), 0 10px 30px rgba(0,0,0,0.04);
  --easing: cubic-bezier(.22,.61,.36,1);
  --speed-fast: 160ms;
  --speed-mid: 220ms;
}

[data-theme="dark"] {
  --bg: #141517;
  --card: #1c1e21;
  --text: #e6e6e6;
  --muted: #a9a9a9;
  --primary: #86a7c4;
  --primary-contrast: #121314;
  --accent: #3a5168;
  --border: #2a2d31;
  --success: #6ccf78;
  --error: #ff5c6c;
  --tint: 58, 81, 104;
  --hl-bg: rgba(255, 221, 87, 0.35);
  --hl-fg: inherit;
}

/* Palettes (light) */
:root[data-palette="cozy"] { /* default, already set via variables above */ }
:root[data-palette="ocean"] { --primary: #2a7fab; --accent: #8ecae6; --tint: 142, 202, 230; }
:root[data-palette="forest"] { --primary: #2e7d32; --accent: #a5d6a7; --tint: 165, 214, 167; }
:root[data-palette="plum"] { --primary: #7b5ea7; --accent: #c8b5e8; --tint: 200, 181, 232; }
:root[data-palette="solar"] { --primary: #b58900; --accent: #f1d38a; --tint: 241, 211, 138; }

/* Palettes (dark) */
[data-theme="dark"][data-palette="ocean"] { --primary: #7fc3e1; --accent: #2a6f8a; --tint: 47, 104, 138; }
[data-theme="dark"][data-palette="forest"] { --primary: #79d68c; --accent: #2a6230; --tint: 42, 98, 48; }
[data-theme="dark"][data-palette="plum"] { --primary: #c9a6ff; --accent: #5b3d7a; --tint: 91, 61, 122; }
[data-theme="dark"][data-palette="solar"] { --primary: #ffd166; --accent: #8b6a00; --tint: 139, 106, 0; }

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, Noto Sans, Arial, "Apple Color Emoji", "Segoe UI Emoji";
  line-height: 1.5;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  transition: background-color var(--speed-mid) var(--easing), color var(--speed-mid) var(--easing);
  padding-top: 72px; /* space for fixed hotbar */
}

/* Fixed top hotbar */
.hotbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  background: rgba(var(--tint), 0.08);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(6px);
  z-index: 1000;
}
.hotbar .brand { display: flex; flex-direction: column; align-items: flex-start; }
.hotbar .brand-link { text-decoration: none; color: inherit; }
.hotbar h1 { margin: 0; font-weight: 700; letter-spacing: 0.2px; font-size: 18px; }
.hotbar .byline { margin-top: 2px; font-size: 12px; color: var(--muted); opacity: 0.9; }
.hotbar .hotbar-actions { display: flex; align-items: center; gap: 14px; }
.gear-btn { text-decoration: none; font-size: 20px; line-height: 1; padding: 6px 8px; border-radius: 10px; }
.gear-btn:hover { background: rgba(var(--tint), 0.15); }

/* Inline highlight */
.hl { background: var(--hl-bg); color: var(--hl-fg); padding: 0 .2em; border-radius: .25em; }

/* Course list tiles (legacy grid left for fallback) */
.course-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; margin-top: 12px; }
.course-card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 12px; box-shadow: var(--elev); display: flex; flex-direction: column; gap: 8px; }
.course-card h3 { margin: 0 0 6px 0; font-size: 16px; }
.course-card .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.course-card a.btn { text-decoration: none; }

/* Tarot spread deck (new home layout) */

/* Connect Nodes (cn-*) */
.cn-form { position: relative; }
.cn-container { position: relative; display: flex; flex-direction: column; gap: 12px; }
.cn-guidance { color: var(--muted); font-size: 14px; }
.cn-grid { position: relative; display: grid; grid-template-columns: 1fr 1fr; gap: 16px; align-items: start; }
.cn-col { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius-sm); box-shadow: var(--elev); padding: 8px; }
.cn-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.cn-node { position: relative; display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 10px 12px; min-height: 44px; border: 1px solid var(--border); border-radius: 12px; background: var(--card); cursor: pointer; transition: background var(--speed-fast) var(--easing), border-color var(--speed-fast) var(--easing), box-shadow var(--speed-fast) var(--easing); }
.cn-node:hover, .cn-node:focus { outline: none; border-color: rgba(var(--tint), 0.5); box-shadow: 0 0 0 3px rgba(var(--tint), 0.15); }
.cn-node.selected { background: rgba(var(--tint), 0.12); border-color: rgba(var(--tint), 0.7); }
.cn-node.connected { background: rgba(var(--tint), 0.08); }
.cn-clear { font-size: 16px; line-height: 1; padding: 4px 8px; border: 1px solid var(--border); border-radius: 10px; background: transparent; cursor: pointer; color: var(--muted); }
.cn-clear:hover { background: rgba(var(--tint), 0.12); color: var(--text); }
.cn-svg { position: absolute; top: 0; left: 0; pointer-events: none; }
.cn-link { fill: none; stroke: rgba(var(--tint), 0.9); stroke-width: 3; stroke-linecap: round; filter: drop-shadow(0 1px 1px rgba(0,0,0,0.08)); }
.cn-actions { display: flex; gap: 8px; justify-content: flex-end; }

@media (max-width: 720px) {
  .cn-grid { grid-template-columns: 1fr; }
}

/* Tarot spread deck (new home layout) */
.tarot-spread {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 0; /* overlap via negative margins on cards */
  perspective: 1200px;
  overflow-x: auto;
  padding: 16px 10px 6px;
  scrollbar-width: none; /* Firefox */
}
.tarot-spread::-webkit-scrollbar { display: none; }

.tarot-card {
  position: relative;
  width: clamp(180px, 18vw, 240px);
  height: clamp(240px, 26vw, 320px);
  background: color-mix(in srgb, var(--card) 80%, rgba(var(--tint), 0.25));
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 4px 18px rgba(0,0,0,0.08), 0 12px 28px rgba(0,0,0,0.06);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  margin-left: -56px; /* overlap */
  padding: 12px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  color: inherit;
  will-change: transform;
  z-index: calc(10 + var(--i));
  outline: none; /* we'll rely on :focus-visible */
}
.tarot-card:first-child { margin-left: 0; }

/* Fan math: set --mid on container via JS: (N-1)/2 */
.tarot-card { --d: calc(var(--i) - var(--mid, 2.5)); }

/* Base transform: rotate by distance from center; slight drop for outer cards */
.tarot-card {
  transform: rotate(calc(var(--d) * 6deg)) translateY(calc(var(--d) * 2px));
  transition: transform var(--speed-mid) var(--easing), box-shadow var(--speed-mid) var(--easing), filter var(--speed-mid) var(--easing);
}

/* Content inside card */
.tarot-card h3 { margin: 4px 0 8px; font-size: 16px; text-align: center; }
.tarot-card .actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.tarot-card a.btn { text-decoration: none; }

/* Hover/focus bump */
.tarot-card:hover, .tarot-card:focus-visible {
  transform: translateY(-22px) rotate(calc(var(--d) * 2deg)) scale(1.06);
  z-index: 999;
  box-shadow: 0 10px 30px rgba(0,0,0,0.20), 0 20px 50px rgba(0,0,0,0.18);
}
/* Restore visible focus outline for accessibility */
.tarot-card:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

/* Optional neighbor push: push following siblings slightly when a card is hovered */
.tarot-card:hover ~ .tarot-card { transform: translateX(12px) rotate(calc(var(--d) * 5deg)) translateY(calc(var(--d) * 2px)); }

/* Responsive tweaks */
@media (max-width: 720px) {
  .tarot-card { margin-left: -42px; width: clamp(160px, 46vw, 220px); height: clamp(220px, 60vw, 280px); }
}
@media (prefers-reduced-motion: reduce) {
  .tarot-card { transition: none; }
}

/* Course landing layout */
.course-layout { display: grid; grid-template-columns: clamp(240px, 22vw, 280px) 1fr; gap: 20px; align-items: start; }
.course-sidebar { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 12px; position: sticky; top: 72px; height: fit-content; }
.course-sidebar .nav-btn { display: block; width: 100%; text-align: left; margin: 6px 0; padding: 10px 12px; border: 1px solid var(--border); background: var(--card); border-radius: 10px; cursor: pointer; text-decoration: none; color: inherit; min-height: 44px; transition: background-color var(--speed-fast) var(--easing), border-color var(--speed-fast) var(--easing); }
.course-sidebar .nav-btn.active, .course-sidebar .nav-btn[aria-current="page"] { background: rgba(var(--tint), 0.10); border-color: rgba(var(--tint), 0.20); }
.course-sidebar .nav-btn:hover { background: rgba(var(--tint), 0.08); }
/* Practice distinct button */
.course-sidebar #btn-practice { background: var(--primary); color: var(--primary-contrast); border-color: transparent; text-align: center; font-weight: 600; }
.course-sidebar #btn-practice:hover { filter: brightness(0.98); }

.course-main { min-height: 50vh; max-width: min(1120px, 75vw); }
/* Breadcrumb in top bar */
.hotbar .crumb { margin: 0 12px; flex: 1; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hotbar .crumb a { color: inherit; text-decoration: none; }

.course-title { margin-top: 0; }
/* Remove floating card feel on main content, keep tidy frame */
.container.is-course .course-content { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 16px; box-shadow: none; }
.course-content .error { color: var(--error); }

/* Drawer toggle (mobile) */
.drawer-btn { display: none; position: sticky; top: 72px; margin-bottom: 10px; margin-left: 4px; width: 40px; height: 40px; border-radius: 12px; border: 1px solid var(--border); background: var(--card); cursor: pointer; }
.drawer-btn::before { content: ""; display: block; width: 18px; height: 18px; margin: 10px auto; background: currentColor; -webkit-mask: radial-gradient(circle at 50% -2px, #000 4px, transparent 4px) top left/100% 6px no-repeat, linear-gradient(#000,#000) center/100% 2px no-repeat, radial-gradient(circle at 50% calc(100% + 2px), #000 4px, transparent 4px) bottom left/100% 6px no-repeat; mask: radial-gradient(circle at 50% -2px, #000 4px, transparent 4px) top left/100% 6px no-repeat, linear-gradient(#000,#000) center/100% 2px no-repeat, radial-gradient(circle at 50% calc(100% + 2px), #000 4px, transparent 4px) bottom left/100% 6px no-repeat; }

@media (max-width: 900px) {
  .course-layout { grid-template-columns: 1fr; }
  .course-sidebar { position: fixed; left: 8px; right: 8px; top: 64px; z-index: 1001; max-width: 320px; transform: translateX(-120%); transition: transform var(--speed-mid) var(--easing); }
  html.drawer-open .course-sidebar { transform: translateX(0); }
  .drawer-btn { display: inline-block; z-index: 1002; }
}

/* Visual loading indicator: any container with aria-busy shows spinner */
[aria-busy="true"] { position: relative; }
[aria-busy="true"]::after {
  content: "";
  position: absolute; inset: 0;
  background: rgba(0,0,0,0.02);
  border-radius: inherit;
  --sz: 20px;
  --bd: 3px;
  pointer-events: none;
}
[aria-busy="true"]::before {
  content: "";
  position: absolute; top: 12px; right: 12px;
  width: var(--sz); height: var(--sz);
  border-radius: 50%;
  border: var(--bd) solid rgba(var(--tint),0.25);
  border-top-color: var(--primary);
  animation: spin var(--speed-mid) linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* Focus visibility across the app */
:root:not(.kbd-nav) :focus { outline: none; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; border-radius: 10px; }

/* Buttons/touch targets */
.btn { min-height: 44px; padding: 10px 14px; border: 1px solid var(--border); border-radius: 12px; background: var(--card); color: inherit; cursor: pointer; transition: background-color var(--speed-fast) var(--easing), transform var(--speed-fast) var(--easing); }
.btn.primary { background: var(--primary); color: var(--primary-contrast); border-color: transparent; }
.btn:hover { background: rgba(var(--tint), 0.08); }
.btn.primary:hover { filter: brightness(0.98); }
.btn:active { transform: translateY(1px); }

/* Reduce motion */
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}

/* Images: minimize CLS */
.question-image img, .explanation-image img { max-width: 100%; height: auto; border-radius: var(--radius-sm); border: 1px solid var(--border); content-visibility: auto; contain-intrinsic-size: 320px 200px; }

.app-footer {
  max-width: 900px;
  margin: 0 auto;
  padding: 16px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.theme-toggle { display: flex; align-items: center; gap: 14px; }
.toggle-group { display: inline-flex; align-items: center; gap: 6px; }
.palette-group { display: inline-flex; align-items: center; gap: 6px; }
.palette-select {
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
}

/* Settings dropdown */
.settings { margin-left: 8px; }
.settings summary { cursor: pointer; font-size: 13px; color: var(--muted); }
.settings-body { padding: 8px 10px; background: var(--card); border: 1px solid var(--border); border-radius: var(--radius-sm); min-width: 260px; }
.settings .option-row { display: flex; align-items: center; gap: 8px; padding: 6px 2px; }

.container {
  max-width: 900px;
  width: 100%;
  margin: 0 auto;
  padding: 0 20px 24px;
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}
/* Course pages use a documentation-like layout, not centered cards */
.container.is-course {
  max-width: 1400px;
  display: block;
}

/* Center cards and make content tidy */
.card {
  width: 100%;
  max-width: 760px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: var(--elev);
}

.hidden { display: none; }

/* Typography */
.card h2 { margin: 4px 0 12px; text-align: center; }
.card p { margin: 8px 0; }

.field { margin: 14px 0; }
.field label { display: block; font-size: 14px; color: var(--muted); margin-bottom: 6px; text-align: center; }
.field select, .field input[type="text"], .field input[type="number"], .field textarea {
  width: 100%;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
}
.field.inline { display: flex; align-items: center; gap: 8px; justify-content: center; }

.checkbox { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; }

.actions { display: flex; gap: 10px; margin-top: 12px; justify-content: center; }

.btn {
  padding: 10px 16px;
  background: #eae7e2;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: transform var(--speed-fast) var(--easing), background var(--speed-mid) var(--easing), box-shadow var(--speed-mid) var(--easing), opacity var(--speed-mid) var(--easing);
  box-shadow: 0 0 0 rgba(0,0,0,0);
}
.btn:hover { transform: translateY(-1px); box-shadow: 0 6px 14px rgba(0,0,0,0.08); }
.btn:active { transform: translateY(0px) scale(0.985); }

.btn.primary {
  background: var(--primary);
  color: var(--primary-contrast);
  border-color: transparent;
}

/* Dark mode: ensure readable text on light button backgrounds without affecting primary or subtle buttons */
[data-theme="dark"] .btn:not(.primary):not(.subtle) { color: #121314; }

.btn.subtle { background: transparent; }

.hint { color: var(--muted); font-size: 13px; text-align: center; }

.quiz-header { display: flex; justify-content: center; align-items: center; gap: 12px; margin-bottom: 8px; }
.progress { color: var(--muted); font-size: 14px; }
.cooldown { color: var(--muted); font-size: 12px; }

.question-meta { color: var(--muted); font-size: 13px; margin-top: 6px; text-align: center; }
.question-text { font-size: 22px; margin: 12px 0 10px; text-align: center; }
.question-image { display: flex; justify-content: center; }
.question-image img { max-width: 100%; border-radius: var(--radius-sm); border: 1px solid var(--border); }

.answer-area { display: grid; gap: 10px; margin: 12px auto; max-width: 680px; }
.option {
  display: flex; align-items: center; gap: 10px; padding: 12px 14px; border: 1px solid var(--border); border-radius: var(--radius-sm); cursor: pointer; background: transparent;
  transition: background var(--speed-mid) var(--easing), transform var(--speed-fast) var(--easing), border-color var(--speed-mid) var(--easing), box-shadow var(--speed-mid) var(--easing);
  -webkit-tap-highlight-color: transparent;
}
.option input { transform: scale(1.15); }
/* Result highlighting when keeping responses */
.option.correct { border-color: rgba(60, 179, 113, 0.7); background: rgba(60, 179, 113, 0.12); }
.option.incorrect { border-color: rgba(220, 20, 60, 0.7); background: rgba(220, 20, 60, 0.10); }
.option.chosen { box-shadow: 0 0 0 2px rgba(var(--tint), 0.25) inset; }
@media (hover: hover) and (pointer: fine) {
  .option:hover { background: rgba(var(--tint), 0.15); }
}
@media (hover: none) {
  .option:hover { background: transparent; }
}
.option:active { transform: scale(0.995); }
/* Keyboard-only focus highlight (no highlight from programmatic focus or touch) */
@supports selector(:has(*)) {
  .kbd-nav .option:has(input:focus-visible) { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(var(--tint), 0.25); }
}
/* Fallback: visible outline on the input itself when using keyboard */
.option input:focus-visible { outline: 2px solid rgba(var(--tint), 0.45); outline-offset: 2px; border-radius: 4px; }

/* Sort (drag-and-drop) question */
.sort-list { display: grid; gap: 8px; }
.sort-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--card);
}
.sort-item.dragging { opacity: 0.6; }
.sort-handle { cursor: grab; user-select: none; opacity: 0.8; }
.sort-label { flex: 1; }
.sort-item.correct { border-color: rgba(60, 179, 113, 0.7); background: rgba(60, 179, 113, 0.12); }
.sort-item.run-correct { box-shadow: 0 0 0 2px rgba(60, 179, 113, 0.35) inset; }

.feedback { margin-top: 10px; font-weight: 700; text-align: center; opacity: 0; transform: translateY(4px); transition: opacity var(--speed-mid) var(--easing), transform var(--speed-mid) var(--easing); }
.feedback.show { opacity: 1; transform: translateY(0); }
.feedback.ok { color: var(--success); }
.feedback.bad { color: var(--error); }

.explanation { margin-top: 10px; border-top: 1px dashed var(--border); padding-top: 10px; }
.explanation-image { margin-top: 8px; display: flex; justify-content: center; }
.explanation-image img { max-width: 100%; border-radius: var(--radius-sm); border: 1px solid var(--border); }

/* Finished summary */
.summary { margin-top: 12px; display: grid; gap: 10px; }
.summary .row { border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 10px; background: var(--card); }
.summary .row .q { font-weight: 600; margin-bottom: 6px; }
.summary .row.ok { border-color: rgba(60, 179, 113, 0.6); }
.summary .row.bad { border-color: rgba(220, 20, 60, 0.6); }
.summary .badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; margin-left: 6px; }
.summary .badge.ok { background: rgba(60, 179, 113, 0.15); color: var(--text); }
.summary .badge.bad { background: rgba(220, 20, 60, 0.15); color: var(--text); }

/* Tap-anywhere hint pulse */
.hint.pulse { animation: pulse 1.6s var(--easing) infinite; }
@keyframes pulse { 0%,100% { opacity: .6 } 50% { opacity: .9 } }

/* Screen/card enter animations */
.enter { animation: fadeInUp var(--speed-mid) var(--easing); }
.enter-soft { animation: fadeIn var(--speed-mid) var(--easing); }
@keyframes fadeInUp { from { opacity: 0; transform: translateY(10px) } to { opacity: 1; transform: translateY(0) } }
@keyframes fadeIn { from { opacity: 0 } to { opacity: 1 } }

/* Toggle switch */
.switch { position: relative; display: inline-block; width: 44px; height: 24px; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; transition: .2s; border-radius: 999px; }
.slider:before { position: absolute; content: ""; height: 18px; width: 18px; left: 3px; bottom: 3px; background-color: white; transition: .2s; border-radius: 50%; }
input:checked + .slider { background-color: var(--primary); }
input:checked + .slider:before { transform: translateX(20px); }

.toggle-label { font-size: 13px; color: var(--muted); margin-left: 6px; }

/* Additional UI for presets/custom choose screen */
.pane { margin-top: 12px; }
.list { display: grid; gap: 10px; margin: 12px 0; }
.item.btn.selected { border-color: var(--primary); background: rgba(155, 183, 207, 0.2); }

/* Responsive */
@media (max-width: 640px) {
  .app-footer, .container { padding: 12px; }
  .card { padding: 18px; border-radius: 14px; }
  .question-text { font-size: 20px; }
}

/* Background layer for custom user image */
.bg-layer {
  position: fixed;
  inset: 0;
  background-position: center center;
  background-repeat: no-repeat;
  background-size: cover;
  opacity: 0; /* will be set via inline style */
  pointer-events: none;
  z-index: 0;
}
