/* ===== Design tokens ===== */
:root {
  --bg: #eef1f6;
  --bg-grad-1: #f4f6fb;
  --bg-grad-2: #dfe6f1;
  --ink: #1f2a37;
  --muted: #64748b;
  --paper: #fffdf8;
  --paper-edge: #efe9dc;
  --primary: #1e3a5f;
  --primary-soft: #2f5680;
  --accent: #d92d20;
  --line: #d7dde8;
  --rule: #c7d2e0;
  --surface: #ffffff;
  --border: #e2e8f0;
  --danger: #d92d20;
  --shadow-lg: 0 24px 60px -20px rgba(15, 30, 55, 0.35);
  --shadow-md: 0 10px 30px -12px rgba(15, 30, 55, 0.25);
  --radius: 16px;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html { background: var(--bg); }

body {
  background:
    radial-gradient(1200px 600px at 80% -10%, rgba(47, 86, 128, 0.10), transparent 60%),
    linear-gradient(160deg, var(--bg-grad-1) 30%, var(--bg-grad-2) 100%);
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  color: var(--ink);
  font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* ===== Header ===== */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 14px;
  padding: 14px clamp(14px, 3vw, 30px);
  padding-top: max(14px, env(safe-area-inset-top));
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-soft) 100%);
  color: #fff;
  box-shadow: var(--shadow-md);
  position: sticky;
  top: 0;
  z-index: 50;
}

.brand { display: flex; align-items: center; gap: 12px; }

.brand-mark {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.14);
  border: 1px solid rgba(255, 255, 255, 0.22);
  color: #fff;
  flex-shrink: 0;
}

.brand-text h1 {
  font-size: clamp(1.1rem, 2.4vw, 1.5rem);
  font-weight: 800;
  letter-spacing: -0.02em;
  line-height: 1.1;
}

.tagline {
  font-size: 0.74rem;
  font-weight: 500;
  color: rgba(255, 255, 255, 0.75);
  margin-top: 2px;
}

/* ===== Toolbar ===== */
#toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.tool-group {
  display: flex;
  align-items: center;
  gap: 8px;
  position: relative;
}

.save-status {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 0.78rem;
  font-weight: 600;
  color: rgba(255, 255, 255, 0.9);
  background: rgba(255, 255, 255, 0.12);
  border: 1px solid rgba(255, 255, 255, 0.18);
  padding: 6px 12px;
  border-radius: 999px;
}
.save-status .dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: #4ade80; box-shadow: 0 0 0 0 rgba(74, 222, 128, 0.6);
}
.save-status.saving .dot { background: #fbbf24; animation: pulse 1s infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(251, 191, 36, 0.6);} 70% { box-shadow: 0 0 0 7px rgba(251, 191, 36, 0);} 100% { box-shadow: 0 0 0 0 rgba(251, 191, 36, 0);} }

.tool-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 8px 13px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  cursor: pointer;
  font-size: 0.82rem;
  font-weight: 600;
  font-family: inherit;
  transition: background 0.18s ease, transform 0.1s ease, border-color 0.18s ease;
}
.tool-btn:hover { background: rgba(255, 255, 255, 0.22); }
.tool-btn:active { transform: translateY(1px); }
.tool-btn.small { padding: 7px; }
.tool-btn.danger:hover { background: var(--danger); border-color: var(--danger); }
.tool-btn[aria-pressed="true"] {
  background: #fff;
  color: var(--primary);
  border-color: #fff;
}

.pen-options {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.16);
  border: 1px solid rgba(255, 255, 255, 0.22);
}
.ink-swatches, .text-swatches { display: inline-flex; align-items: center; gap: 6px; }
.ink-swatch {
  width: 20px; height: 20px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.7);
  background: var(--ink);
  cursor: pointer;
  padding: 0;
  transition: transform 0.12s ease;
}
.ink-swatch.active { transform: scale(1.25); box-shadow: 0 0 0 2px #fff; }
.pen-size { width: 80px; accent-color: #fff; cursor: pointer; }

.text-swatch {
  width: 20px; height: 20px;
  border-radius: 6px;
  border: 2px solid rgba(255, 255, 255, 0.7);
  background: var(--ink);
  cursor: pointer;
  padding: 0;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}
.text-swatch:hover { transform: scale(1.18); }
.text-swatch.active { transform: scale(1.18); box-shadow: 0 0 0 2px #fff; }

.field {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 0.78rem;
  font-weight: 600;
  color: rgba(255, 255, 255, 0.9);
}
.field select {
  padding: 7px 10px;
  border-radius: 9px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  outline: none;
}
.field select option { color: #1f2a37; }

/* ===== Main layout ===== */
main {
  display: flex;
  flex: 1;
  gap: 18px;
  padding: clamp(12px, 2.4vw, 24px);
  min-height: 0;
  height: calc(100vh - 78px);
  height: calc(100dvh - 78px);
}

/* ===== Notebook ===== */
#notebook-section {
  display: flex;
  flex: 5;
  gap: 0;
  background: linear-gradient(180deg, #fffdf8, #fbf7ee);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: clamp(14px, 2vw, 26px);
  position: relative;
  border: 1px solid var(--paper-edge);
  overflow: hidden;
}

.spine {
  width: 2px;
  margin: 6px 0;
  background: linear-gradient(180deg, transparent, rgba(31, 42, 55, 0.12), transparent);
  flex-shrink: 0;
}

.page {
  flex: 1;
  position: relative;
  background: var(--paper);
  border-radius: 10px;
  overflow: hidden;
  min-width: 0;
}
.left-page { border-right: 1px solid rgba(31, 42, 55, 0.04); border-radius: 10px 4px 4px 10px; box-shadow: inset -16px 0 24px -20px rgba(31,42,55,0.25); }
.right-page { border-left: 1px solid rgba(31, 42, 55, 0.04); border-radius: 4px 10px 10px 4px; box-shadow: inset 16px 0 24px -20px rgba(31,42,55,0.25); }

.page-tab {
  position: absolute;
  bottom: 6px;
  right: 14px;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--muted);
  opacity: 0.6;
  z-index: 3;
  pointer-events: none;
}

/* ruled lines */
.page::before {
  content: "";
  position: absolute;
  inset: 0;
  background-image: repeating-linear-gradient(
    to bottom,
    transparent,
    transparent calc(var(--rule-h, 30px) - 1px),
    var(--rule) calc(var(--rule-h, 30px) - 1px),
    var(--rule) var(--rule-h, 30px)
  );
  background-position: 0 var(--rule-top, 18px);
  pointer-events: none;
  z-index: 0;
  opacity: 0.55;
}
/* red margin line */
.page::after {
  content: "";
  position: absolute;
  top: 0; bottom: 0;
  left: 42px;
  width: 1.5px;
  background: rgba(217, 45, 32, 0.22);
  pointer-events: none;
  z-index: 0;
}

.drawing-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 2;
  touch-action: none;
  pointer-events: none;
}
body.pen-mode .drawing-canvas { pointer-events: auto; cursor: crosshair; }

.note-area {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  background: transparent;
  border: none;
  padding: 18px 24px 18px 54px;
  font-size: var(--note-size, 17px);
  line-height: var(--rule-h, 30px);
  color: var(--ink);
  outline: none;
  z-index: 1;
  font-family: var(--note-font, 'Caveat', 'Inter', cursive);
  font-weight: var(--note-weight, 600);
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: break-word;
  overflow-y: auto;
  letter-spacing: var(--note-spacing, 0.2px);
  cursor: text;
}
.note-area:empty::before {
  content: attr(data-placeholder);
  color: rgba(100, 116, 139, 0.5);
  font-weight: 500;
  pointer-events: none;
}
body.pen-mode .note-area { pointer-events: none; }

/* ===== Dictionary ===== */
#dictionary-section {
  flex: 2.2;
  min-width: 260px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  padding: 16px;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  overflow: hidden;
}

.flag-container {
  display: flex;
  justify-content: center;
  gap: 18px;
  margin-bottom: 14px;
}
.flag-btn {
  padding: 4px;
  border: 2px solid transparent;
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
  transition: transform 0.18s ease, border-color 0.18s ease, box-shadow 0.18s ease;
  line-height: 0;
}
.flag {
  width: 52px;
  height: 52px;
  object-fit: cover;
  object-position: center;
  border-radius: 50%;
  background: transparent;
  filter: grayscale(85%) opacity(0.6);
  transition: filter 0.25s ease;
}
.flag-btn.active { border-color: var(--primary); box-shadow: 0 6px 18px -6px rgba(30, 58, 95, 0.5); transform: translateY(-2px); }
.flag-btn.active .flag { filter: none; }
.flag-btn:hover .flag { filter: grayscale(30%); }

.dict-search {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--muted);
  margin-bottom: 12px;
  background: #f8fafc;
}
.dict-search input {
  border: none;
  outline: none;
  background: transparent;
  font-size: 0.9rem;
  font-family: inherit;
  width: 100%;
  color: var(--ink);
}

#dictionary {
  flex: 1;
  overflow-y: auto;
  min-height: 0;
  margin: 0 -4px;
  padding: 0 4px;
}
.dictionary-list { display: none; }
.dictionary-list.active { display: block; }

.dict-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 11px 12px;
  border-radius: 10px;
  font-size: 0.95rem;
  font-weight: 500;
  transition: background 0.15s ease;
}
.dict-row:nth-child(odd) { background: #f8fafc; }
.dict-row:hover { background: #eef2f8; }
.dict-row .word { word-break: break-word; }
.dict-row .word small { display: block; color: var(--muted); font-weight: 500; font-size: 0.78rem; margin-top: 2px; }
.dict-del {
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  border-radius: 8px;
  padding: 5px;
  display: grid;
  place-items: center;
  opacity: 0;
  transition: opacity 0.15s ease, color 0.15s ease, background 0.15s ease;
  flex-shrink: 0;
}
.dict-row:hover .dict-del { opacity: 1; }
.dict-del:hover { color: var(--danger); background: rgba(217, 45, 32, 0.1); }

.empty-state {
  text-align: center;
  color: var(--muted);
  font-size: 0.88rem;
  padding: 30px 12px;
}

/* ===== Form ===== */
#word-form {
  margin-top: 12px;
  display: flex;
  gap: 8px;
}
#word-form input {
  flex: 1;
  min-width: 0;
  padding: 11px 13px;
  font-size: 0.9rem;
  font-family: inherit;
  border: 1px solid var(--border);
  border-radius: 10px;
  outline: none;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}
#word-form input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(30, 58, 95, 0.12); }
#word-form button {
  background: var(--primary);
  color: #fff;
  border: none;
  padding: 0 16px;
  border-radius: 10px;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background 0.18s ease, transform 0.1s ease;
  flex-shrink: 0;
}
#word-form button:hover { background: var(--primary-soft); }
#word-form button:active { transform: translateY(1px); }

/* ===== Toast ===== */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 20px);
  background: var(--ink);
  color: #fff;
  padding: 11px 20px;
  border-radius: 999px;
  font-size: 0.86rem;
  font-weight: 600;
  box-shadow: var(--shadow-md);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
  z-index: 100;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ===== Scrollbars ===== */
.note-area::-webkit-scrollbar, #dictionary::-webkit-scrollbar { width: 8px; }
.note-area::-webkit-scrollbar-thumb, #dictionary::-webkit-scrollbar-thumb { background: rgba(100,116,139,0.3); border-radius: 8px; }

/* ===== Responsive ===== */
@media (max-width: 900px) {
  main {
    flex-direction: column;
    height: auto;
    overflow: visible;
    gap: 16px;
  }
  #notebook-section { min-height: 420px; }
  #dictionary-section { min-height: 360px; max-height: 70vh; }
  .tagline { display: none; }
}

@media (max-width: 600px) {
  .app-header { gap: 10px; }
  #toolbar { width: 100%; }
  .tool-btn span { display: none; }
  .tool-btn { padding: 9px; }
  .pen-options { flex-wrap: wrap; }
  .page::after { left: 36px; }
  .note-area { padding-left: 46px; }
  #notebook-section .page-tab { display: none; }
}

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