@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@400;600;700&display=swap');

/* ==========================================================================
   1. LIGHT THEME (Default: White, Soft Grey, and Orange)
   ========================================================================== */
:root {
  --accent: #f97316;
  --accent-hover: #ea580c;
  --accent-light: #fff7ed;
  --accent-glow: rgba(249, 115, 22, 0.35);

  --topbar-bg: #ffffff;
  --topbar-border: #e5e7eb;
  --topbar-text: #18181b;

  --canvas-bg: #f4f4f6;
  --canvas-dot: rgba(24, 24, 27, 0.12);

  --sidebar-bg: #ffffff;
  --sidebar-secondary: #f8f9fa;
  --sidebar-border: #e5e7eb;

  --pill-bg: #ffffff;
  --pill-border: #e4e4e7;
  --pill-hover-border: #f97316;
  --pill-shadow: 0 2px 6px rgba(0, 0, 0, 0.06);

  --text-dark: #18181b;
  --text-muted: #71717a;
  --danger: #ef4444;

  --radius-pill: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, "Vazirmatn", sans-serif;
  --ui-scale: 1;
}

/* ==========================================================================
   2. DARK THEME (Dark Grey/Charcoal + Vibrant Orange)
   ========================================================================== */
html[data-theme="dark"] {
  --accent: #fb923c;                        /* Slightly brighter orange for dark backgrounds */
  --accent-hover: #f97316;
  --accent-light: rgba(249, 115, 22, 0.15); /* Subtle orange glow tint */
  --accent-glow: rgba(251, 146, 60, 0.45);

  --topbar-bg: #18181b;                     /* Deep zinc/grey topbar */
  --topbar-border: #27272a;
  --topbar-text: #f4f4f5;

  --canvas-bg: #121215;                     /* Deep dark workspace */
  --canvas-dot: rgba(255, 255, 255, 0.08);  /* Subtle light dots */

  --sidebar-bg: #18181b;                    /* Dark sidebar & dictionary */
  --sidebar-secondary: #202024;             /* Slightly lighter dark grey for headers */
  --sidebar-border: #27272a;

  --pill-bg: #27272a;                       /* Dark grey element pills */
  --pill-border: #3f3f46;
  --pill-hover-border: #fb923c;
  --pill-shadow: 0 3px 8px rgba(0, 0, 0, 0.35);

  --text-dark: #f4f4f5;                     /* Crisp off-white text */
  --text-muted: #a1a1aa;                    /* Muted silver-grey text */
  --danger: #f87171;
}
/* Optional: Make the dictionary icon visible on dark pills */
html[data-theme="dark"] .dict-btn img {
  filter: invert(0.85);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  font-family: var(--font);
  color: var(--text-dark);
  background: var(--canvas-bg);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

button, input, select, textarea {
  font-family: inherit;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

/* ---------- Topbar (Matches Main Page Header) ---------- */
#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 24px;
  background: var(--topbar-bg);
  color: var(--topbar-text);
  border-bottom: 1px solid var(--topbar-border);
  flex: 0 0 auto;
  z-index: 5000;
}

/* Clickable Brand Logo linking to Main Page */
.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  text-decoration: none;
  color: var(--topbar-text);
  padding: 4px 8px;
  margin-left: -8px;
  border-radius: 8px;
  transition: background 0.15s ease;
}

.brand:hover {
  background: var(--accent-light);
}

.brand-emoji { font-size: 20px; }

.brand-name {
  font-weight: 800;
  font-size: calc(17px * var(--ui-scale));
  letter-spacing: -0.2px;
  color: var(--text-dark);
}

.brand-divider {
  color: var(--topbar-border);
  font-weight: 400;
  margin: 0 2px;
}

.brand-site {
  font-weight: 700;
  font-size: calc(15px * var(--ui-scale));
  color: var(--text-dark);
}

.brand-site em {
  font-style: normal;
  color: var(--accent);
}

.brand-sub {
  font-size: 12px;
  color: var(--text-muted);
  margin-left: 4px;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.home-link {
  text-decoration: none;
  color: var(--text-muted);
  font-size: calc(13.5px * var(--ui-scale));
  font-weight: 600;
  padding: 6px 12px;
  border-radius: 8px;
  transition: color 0.15s ease, background 0.15s ease;
}

.home-link:hover {
  color: var(--accent);
  background: var(--accent-light);
}

.topbar-actions button {
  background: var(--sidebar-secondary);
  color: var(--text-dark);
  border: 1px solid var(--pill-border);
  border-radius: 8px;
  padding: calc(7px * var(--ui-scale)) calc(12px * var(--ui-scale));
  font-size: calc(13px * var(--ui-scale));
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
}

.topbar-actions button:hover {
  background: var(--accent-light);
  border-color: var(--accent);
  color: var(--accent-hover);
}

.topbar-actions button:active {
  transform: translateY(1px);
}

#reset-btn:hover {
  background: #fef2f2;
  border-color: var(--danger);
  color: var(--danger);
}

/* ---------- Drop-down menu in the top right corner ---------- */
.menu-wrap { position: relative; }
.topbar-actions .menu-btn {
  line-height: 1;
  font-size: calc(18px * var(--ui-scale));
  padding: calc(6px * var(--ui-scale)) calc(11px * var(--ui-scale));
}

.menu-panel {
  position: absolute;
  right: 0;
  top: calc(100% + 8px);
  z-index: 300;
  min-width: 230px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  background: var(--sidebar-bg);
  border: 1px solid var(--sidebar-border);
  border-radius: 12px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.12);
}

.topbar-actions .menu-item { width: 100%; text-align: left; }
.menu-sep { height: 1px; margin: 4px 0; background: var(--sidebar-border); }

.menu-scale {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 4px 4px 2px;
  font-size: 13px;
  color: var(--text-dark);
}

.menu-scale-label { display: flex; justify-content: space-between; }
.menu-scale-label b { color: var(--accent); font-weight: 700; }
.menu-scale input[type="range"] { width: 100%; margin: 0; accent-color: var(--accent); }

/* ---------- Main layout ---------- */
#main {
  flex: 1 1 auto;
  display: flex;
  min-height: 0;
}

/* ---------- Canvas (crafting workspace) ---------- */
#canvas-wrap {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
}

#canvas {
  position: absolute;
  inset: 0;
  overflow: hidden;
  background-color: var(--canvas-bg);
  background-image: radial-gradient(var(--canvas-dot) 1.4px, transparent 1.4px);
  background-size: 22px 22px;
  touch-action: none;
}

#canvas-hint {
  position: absolute;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  color: var(--text-muted);
  font-size: 13px;
  pointer-events: none;
  margin: 0;
  user-select: none;
  text-align: center;
  width: 90%;
}

/* An element sitting on the canvas */
.instance {
  position: absolute;
  transform: translate(-50%, -50%);
  display: flex;
  align-items: center;
  gap: calc(6px * var(--ui-scale));
  padding: calc(8px * var(--ui-scale)) calc(14px * var(--ui-scale));
  background: var(--pill-bg);
  border: 1px solid var(--pill-border);
  border-radius: var(--radius-pill);
  box-shadow: var(--pill-shadow);
  font-size: calc(15px * var(--ui-scale));
  font-weight: 500;
  white-space: nowrap;
  cursor: grab;
  user-select: none;
  touch-action: none;
  z-index: 1;
  transition: box-shadow 0.15s ease, border-color 0.15s ease;
}

.instance:hover {
  border-color: var(--pill-hover-border);
}

.instance.dragging {
  cursor: grabbing;
  border-color: var(--accent);
  box-shadow: 0 8px 20px rgba(249, 115, 22, 0.22);
  z-index: 50;
}

.instance.pending {
  opacity: 0.55;
  pointer-events: none;
  animation: pulse 0.9s ease-in-out infinite;
}

.instance.merge-target {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow), var(--pill-shadow);
}

.instance.spawn-pop { animation: pop-in 0.22s ease-out; }
.instance.first-discovery { animation: pop-in 0.22s ease-out, glow-ring 1.4s ease-out; }

@keyframes pulse {
  0%, 100% { opacity: 0.5; }
  50% { opacity: 0.85; }
}

@keyframes pop-in {
  0% { transform: translate(-50%, -50%) scale(0.4); opacity: 0; }
  70% { transform: translate(-50%, -50%) scale(1.08); opacity: 1; }
  100% { transform: translate(-50%, -50%) scale(1); opacity: 1; }
}

@keyframes glow-ring {
  0% { box-shadow: 0 0 0 0 var(--accent-glow), var(--pill-shadow); }
  60% { box-shadow: 0 0 0 14px transparent, var(--pill-shadow); }
  100% { box-shadow: var(--pill-shadow); }
}

/* Ghost element while dragging from sidebar */
.drag-ghost {
  position: fixed;
  pointer-events: none;
  display: flex;
  align-items: center;
  gap: calc(6px * var(--ui-scale));
  padding: calc(8px * var(--ui-scale)) calc(14px * var(--ui-scale));
  background: var(--pill-bg);
  border: 1px solid var(--accent);
  border-radius: var(--radius-pill);
  box-shadow: 0 8px 20px rgba(249, 115, 22, 0.22);
  font-size: calc(15px * var(--ui-scale));
  font-weight: 500;
  white-space: nowrap;
  transform: translate(-50%, -50%);
  z-index: 100;
  opacity: 0.95;
}

.emoji { font-size: calc(16px * var(--ui-scale)); line-height: 1; }

/* ---------- Sidebar ---------- */
#sidebar {
  flex: 0 0 var(--sidebar-w, 300px);
  background: var(--sidebar-bg);
  border-left: 1px solid var(--sidebar-border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

#sidebar-search-wrap {
  padding: calc(12px * var(--ui-scale));
  background: var(--sidebar-secondary);
  border-bottom: 1px solid var(--sidebar-border);
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
}

#sidebar-search {
  flex: 1 1 auto;
  min-width: 0;
  padding: calc(8px * var(--ui-scale)) calc(12px * var(--ui-scale));
  border-radius: var(--radius-pill);
  border: 1px solid var(--pill-border);
  background: var(--pill-bg);
  color: var(--text-dark);
  font-size: calc(13.5px * var(--ui-scale));
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

#sidebar-search:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(249, 115, 22, 0.15);
}

#sidebar-count {
  font-size: calc(12px * var(--ui-scale));
  color: var(--text-muted);
  white-space: nowrap;
}

#sidebar-list {
  flex: 1 1 auto;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: calc(12px * var(--ui-scale));
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: calc(8px * var(--ui-scale));
}

.sidebar-item {
  display: flex;
  align-items: center;
  gap: calc(6px * var(--ui-scale));
  padding: calc(7px * var(--ui-scale)) calc(13px * var(--ui-scale));
  background: var(--pill-bg);
  border: 1px solid var(--pill-border);
  border-radius: var(--radius-pill);
  box-shadow: var(--pill-shadow);
  font-size: calc(14px * var(--ui-scale));
  font-weight: 500;
  cursor: grab;
  user-select: none;
  touch-action: pan-y;
  height: fit-content;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.sidebar-item:hover {
  border-color: var(--pill-hover-border);
  background: var(--accent-light);
}

.sidebar-item:active { cursor: grabbing; }
.sidebar-item.newly-added { animation: pop-in 0.25s ease-out; border-color: var(--accent); }
.sidebar-item .label { min-width: 0; overflow-wrap: anywhere; hyphens: auto; }

.sidebar-empty {
  color: var(--text-muted);
  font-size: 13px;
  padding: 8px 4px;
}

/* ---------- Dictionary Button & Panel ---------- */
.dict-btn {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  width: calc(22px * var(--ui-scale));
  height: calc(22px * var(--ui-scale));
  margin-left: calc(2px * var(--ui-scale));
  padding: 0;
  border: none;
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
  transition: background 0.15s ease;
}

.dict-btn:hover { background: rgba(249, 115, 22, 0.15); }

.dict-btn img {
  width: calc(20px * var(--ui-scale));
  height: calc(20px * var(--ui-scale));
  display: block;
  pointer-events: none;
}

#dict-panel {
  flex: 0 0 var(--dict-w, 380px);
  display: none;
  flex-direction: column;
  min-width: 0;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--sidebar-border);
  container: dict / inline-size;
}

#dict-panel.open { display: flex; }

.dict-header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(12px * var(--ui-scale)) calc(14px * var(--ui-scale));
  background: var(--sidebar-secondary);
  border-bottom: 1px solid var(--sidebar-border);
}

.dict-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: calc(18px * var(--ui-scale));
  font-weight: 700;
  min-width: 0;
}

.dict-title span:last-child { overflow-wrap: anywhere; }
.dict-title .emoji { font-size: calc(22px * var(--ui-scale)); }

.dict-close {
  border: none;
  background: transparent;
  font-size: calc(16px * var(--ui-scale));
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
}

.dict-close:hover {
  background: var(--accent-light);
  color: var(--accent);
}

.dict-pron {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: calc(8px * var(--ui-scale));
  padding: calc(10px * var(--ui-scale)) calc(14px * var(--ui-scale));
  border-bottom: 1px solid var(--sidebar-border);
}

.dict-pron-label {
  margin-right: auto;
  font-size: calc(12px * var(--ui-scale));
  letter-spacing: 0.6px;
  text-transform: uppercase;
  color: var(--text-muted);
}

.dict-audio-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: calc(6px * var(--ui-scale)) calc(12px * var(--ui-scale));
  border: 1px solid var(--pill-border);
  border-radius: var(--radius-pill);
  background: var(--sidebar-bg);
  color: var(--text-dark);
  font-size: calc(13px * var(--ui-scale));
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
}

.dict-audio-btn svg {
  width: calc(16px * var(--ui-scale));
  height: calc(16px * var(--ui-scale));
}

.dict-audio-btn:hover:not(:disabled) {
  background: var(--accent-light);
  border-color: var(--accent);
  color: var(--accent-hover);
}

.dict-audio-btn.playing {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.dict-audio-btn:disabled { opacity: 0.45; cursor: not-allowed; }

.dict-tabs {
  flex: 0 0 auto;
  display: flex;
  gap: 4px;
  margin: calc(10px * var(--ui-scale)) calc(14px * var(--ui-scale)) 0;
  padding: 3px;
  background: var(--sidebar-bg);
  border-radius: 10px;
}

.dict-tab {
  flex: 1 1 0;
  padding: calc(7px * var(--ui-scale)) calc(10px * var(--ui-scale));
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text-muted);
  font-size: calc(13.5px * var(--ui-scale));
  font-weight: 600;
  cursor: pointer;
}

.dict-tab:hover { color: var(--text-dark); }

.dict-tab.active {
  background: var(--sidebar-bg);
  color: var(--accent);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.dict-tab.empty:not(.active) { opacity: 0.6; }

.dict-body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: calc(14px * var(--ui-scale)) calc(16px * var(--ui-scale)) calc(20px * var(--ui-scale));
}

.dict-status { color: var(--text-muted); font-size: calc(13.5px * var(--ui-scale)); margin: 8px 0; }
.dict-error { color: var(--danger); }
.dict-entry { margin-bottom: 14px; }
.dict-headword { font-size: calc(17px * var(--ui-scale)); font-weight: 700; color: var(--text-dark); }
.dict-pos { font-size: calc(12px * var(--ui-scale)); font-style: italic; font-weight: 400; color: var(--accent); margin-left: 6px; }
.dict-ai {
  display: inline-block;
  margin-left: 8px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent-light);
  color: var(--accent-hover);
  font-size: calc(11px * var(--ui-scale));
  font-weight: 600;
  vertical-align: middle;
}

.dict-senses { margin: 6px 0 0; padding: 0 22px; }
.dict-senses > li { margin-bottom: 8px; font-size: calc(14.5px * var(--ui-scale)); line-height: 1.55; }
.dict-def { margin: 0; }
.dict-examples { margin: 4px 0 0; padding: 0 18px; font-size: calc(13px * var(--ui-scale)); color: var(--text-muted); }

/* ---------- Drag handles between the panels ---------- */
.resizer {
  flex: 0 0 6px;
  margin: 0 -3px;
  position: relative;
  z-index: 60;
  cursor: col-resize;
  touch-action: none;
}

.resizer::after {
  content: "";
  position: absolute;
  inset: 0 2px;
  background: transparent;
  transition: background 0.15s ease;
}

.resizer:hover::after,
#main.resizing .resizer::after {
  background: var(--accent);
  opacity: 0.7;
}

#main.resizing { cursor: col-resize; user-select: none; }

/* ---------- Toasts ---------- */
#toast-stack {
  position: fixed;
  top: 64px;
  right: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 200;
  pointer-events: none;
}

.toast {
  background: var(--sidebar-bg);
  color: var(--text-dark);
  border: 1px solid var(--sidebar-border);
  border-left: 4px solid var(--accent);
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 13.5px;
  font-weight: 600;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
  animation: toast-in 0.2s ease-out, toast-out 0.3s ease-in 2.7s forwards;
  max-width: 280px;
}

.toast.discovery { border-left-color: var(--accent); background: var(--accent-light); }
.toast.error { border-left-color: var(--danger); }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(-6px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes toast-out {
  to { opacity: 0; transform: translateY(-6px); }
}

/* ---------- Responsive & Container Queries ---------- */
@container dict (max-width: 300px) {
  .dict-pron-label { display: none; }
  .dict-pron { justify-content: center; padding: calc(8px * var(--ui-scale)) calc(10px * var(--ui-scale)); }
  .dict-header { padding: calc(10px * var(--ui-scale)); }
  .dict-title { font-size: calc(16px * var(--ui-scale)); }
  .dict-tabs { margin: calc(8px * var(--ui-scale)) calc(10px * var(--ui-scale)) 0; }
  .dict-body { padding: calc(10px * var(--ui-scale)) calc(10px * var(--ui-scale)) calc(16px * var(--ui-scale)); }
  .dict-headword { font-size: calc(16px * var(--ui-scale)); }
  .dict-senses { padding: 0 18px; }
}

@media (max-width: 720px), (pointer: coarse) {
  .resizer { flex-basis: 8px; margin: 0 -4px; }
  .resizer::before { content: ""; position: absolute; inset: 0 -12px; }
  .resizer::after {
    inset: auto;
    left: 50%;
    top: 50%;
    width: 4px;
    height: 44px;
    transform: translate(-50%, -50%);
    border-radius: 2px;
    background: #cbd5e1;
    opacity: 0.9;
  }
  .dict-btn { width: calc(28px * var(--ui-scale)); height: calc(28px * var(--ui-scale)); }
}

@media (max-width: 720px) {
  .instance, .drag-ghost { padding: calc(6px * var(--ui-scale)) calc(12px * var(--ui-scale)); gap: calc(5px * var(--ui-scale)); }
  .sidebar-item { padding: calc(4px * var(--ui-scale)) calc(5px * var(--ui-scale)) calc(4px * var(--ui-scale)) calc(11px * var(--ui-scale)); gap: calc(5px * var(--ui-scale)); }
  .dict-btn { width: calc(26px * var(--ui-scale)); height: calc(26px * var(--ui-scale)); margin-left: 0; }
  .dict-btn img { width: calc(18px * var(--ui-scale)); height: calc(18px * var(--ui-scale)); }
  #topbar { padding: 8px 12px; }
  .brand-sub { display: none; }
  #toast-stack { top: 54px; right: 12px; }
  .toast { max-width: calc(100vw - 24px); }
  #canvas-hint { font-size: 11.5px; width: 80%; }
}

@media (max-width: 720px) and (orientation: portrait) {
  #main { flex-direction: column; }
  #dict-panel {
    flex: 0 0 var(--dict-h, 40%);
    border-right: none;
    border-bottom: 1px solid var(--sidebar-border);
    overflow-y: auto;
    overflow-x: hidden;
  }
  #sidebar {
    flex: 0 0 var(--sidebar-h, 34%);
    border-left: none;
    border-top: 1px solid var(--sidebar-border);
  }
  #canvas-wrap, #dict-panel, #sidebar { min-height: 0; }
  #dict-panel .dict-header, #dict-panel .dict-pron, #dict-panel .dict-tabs, #dict-panel .dict-body { flex: 0 0 auto; }
  #dict-panel .dict-body { overflow: visible; }
  .resizer { flex: 0 0 8px; margin: -4px 0; cursor: row-resize; }
  .resizer::before { inset: -12px 0; }
  .resizer::after { width: 44px; height: 4px; }
  #main.resizing { cursor: row-resize; }
}

@media (max-width: 440px) {
  .brand-divider, .brand-site { display: none; }
}