:root {
  --bg: #f6f8f8;
  --surface: #ffffff;
  --ink: #0d1f23;
  --muted: #6b8086;
  --accent: #0e5e63;
  --accent-soft: #e6f4f0;
  --accent2: #c94a36;
  --border: #dde6e5;
  --danger: #b3423a;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(13, 31, 35, .08), 0 4px 16px rgba(13, 31, 35, .06);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
  line-height: 1.45;
}

button { font: inherit; cursor: pointer; }
input, textarea, select { font: inherit; color: inherit; }

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 20px;
  background: var(--accent);
  color: #fff;
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 8px; cursor: pointer; font-weight: 700; font-size: 18px; }
.topnav { display: flex; gap: 6px; flex-wrap: wrap; }
.topnav button {
  background: rgba(255,255,255,.12);
  border: 1px solid rgba(255,255,255,.25);
  color: #fff;
  border-radius: 8px;
  padding: 6px 14px;
}
.topnav button:hover { background: rgba(255,255,255,.22); }
.topnav button.active { background: #fff; color: var(--accent); font-weight: 600; }

main { max-width: 980px; margin: 0 auto; padding: 24px 16px 80px; }

/* ---------- dashboard ---------- */
.view-title { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin: 6px 0 16px; }
.view-title h1 { font-size: 22px; margin: 0; white-space: nowrap; }
.view-title > div { display: flex; gap: 8px; flex-wrap: wrap; }
.view-title .btn { white-space: nowrap; }

.btn {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: 7px 14px;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 3px 10px; font-size: 13px; }
.btn.ghost { border: none; background: none; color: var(--muted); }
.btn.ghost:hover { color: var(--ink); }

.search {
  width: 100%;
  padding: 11px 16px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  box-shadow: var(--shadow);
  margin-bottom: 14px;
}

.cat-row { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 18px; }
.chip {
  border: 1px solid var(--accent2);
  color: var(--accent2);
  background: var(--surface);
  border-radius: 999px;
  padding: 4px 14px;
  font-size: 13px;
}
.chip.on { background: var(--accent2); color: #fff; }

.tpl-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.tpl-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 4px;
  transition: border-color .15s;
}
.tpl-card:hover { border-color: var(--accent); }
.tpl-card .tpl-name { font-weight: 600; font-size: 16px; }
.tpl-card .tpl-meta { color: var(--muted); font-size: 13px; }
.tpl-card .tpl-actions { display: flex; gap: 6px; margin-top: 8px; }

/* ---------- notes list ---------- */
.tabs { display: inline-flex; background: var(--accent-soft); border-radius: 999px; padding: 4px; margin-bottom: 16px; gap: 2px; }
.tabs button { border: none; background: none; border-radius: 999px; padding: 6px 16px; color: var(--ink); }
.tabs button.active { background: var(--accent); color: #fff; font-weight: 600; }

.note-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  margin-bottom: 8px;
  cursor: pointer;
}
.note-row:hover { border-color: var(--accent); }
.note-row .nr-title { font-weight: 600; }
.note-row .nr-meta { color: var(--muted); font-size: 13px; }

.empty { color: var(--muted); text-align: center; padding: 40px 0; }

/* ---------- note editor ---------- */
.editor-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}
.editor-bar h1 { font-size: 19px; margin: 0; flex: 1; min-width: 200px; }

.note-paper {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 10px 0;
}

.note-line {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 9px 14px;
  border-bottom: 1px dashed #edf1f2;
}
.note-line:last-of-type { border-bottom: none; }
.note-line:hover { background: #f7fafa; }
.nl-text {
  flex: 1;
  min-height: 22px;
  outline: none;
  white-space: pre-wrap;
}
.nl-text:focus { background: var(--accent-soft); border-radius: 6px; }
.nl-tools { display: flex; gap: 2px; opacity: 0; transition: opacity .12s; }
.note-line:hover .nl-tools { opacity: 1; }
.icon-btn {
  border: none;
  background: none;
  color: var(--muted);
  border-radius: 6px;
  padding: 2px 6px;
  font-size: 14px;
}
.icon-btn:hover { background: var(--accent-soft); color: var(--accent); }

.blk {
  display: inline-block;
  border: 1px solid var(--accent2);
  color: var(--accent2);
  border-radius: 8px;
  padding: 1px 10px;
  margin: 0 2px;
  cursor: pointer;
  user-select: none;
  font-size: 14px;
  background: #fdf2f0;
}
.blk.filled { border-style: solid; background: #fbe4e0; color: #b3402f; }
.blk:hover { filter: brightness(.96); }

.addline { padding: 10px 14px; }

.copy-bar { display: flex; gap: 10px; margin-top: 14px; align-items: center; }
.copy-flash { color: var(--accent); font-weight: 600; display: none; }

/* ---------- popover / modal ---------- */
.overlay {
  position: fixed; inset: 0;
  background: rgba(31,42,46,.45);
  display: flex; align-items: center; justify-content: center;
  z-index: 50;
  padding: 16px;
}
.modal {
  background: var(--surface);
  border-radius: 14px;
  padding: 22px;
  max-width: 560px;
  width: 100%;
  max-height: 85vh;
  overflow: auto;
  box-shadow: 0 12px 40px rgba(0,0,0,.25);
  position: relative;
}
.modal-x {
  position: absolute;
  top: 10px;
  right: 12px;
  border: none;
  background: none;
  color: var(--muted);
  font-size: 16px;
  padding: 4px 8px;
  border-radius: 6px;
}
.modal-x:hover { background: var(--accent-soft); color: var(--ink); }
.modal h2 { margin: 0 0 14px; font-size: 19px; }
.modal .opt-grid { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }
.opt {
  border: 1px solid var(--accent2);
  color: var(--accent2);
  background: var(--surface);
  border-radius: 8px;
  padding: 6px 14px;
}
.opt.sel { background: var(--accent2); color: #fff; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }
.field { margin-bottom: 12px; }
.field label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.field input[type=text], .field textarea, .field select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
}

/* ---------- tooth selector ---------- */
.tooth-grid { display: grid; grid-template-columns: repeat(16, 1fr); gap: 3px; margin-bottom: 6px; }
.tooth-grid.lower { margin-bottom: 16px; }
.tooth {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 0;
  text-align: center;
  font-size: 11px;
  background: var(--surface);
  cursor: pointer;
}
.tooth.sel { background: var(--accent); color: #fff; border-color: var(--accent); }
.arch-label { font-size: 12px; color: var(--muted); margin: 4px 0; }

/* ---------- template builder ---------- */
.builder-layout { display: grid; grid-template-columns: 190px 1fr; gap: 16px; align-items: start; }
@media (max-width: 720px) { .builder-layout { grid-template-columns: 1fr; } }

.palette {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  position: sticky;
  top: 70px;
}
.palette h3 { margin: 0 0 10px; font-size: 14px; color: var(--accent); }
.palette .pal-btn {
  display: block;
  width: 100%;
  margin-bottom: 8px;
  border: 1px dashed var(--accent2);
  color: var(--accent2);
  background: #fefaf9;
  border-radius: 10px;
  padding: 10px 8px;
  text-align: center;
  font-size: 13px;
}
.palette .pal-btn:hover { background: #fbe4e0; }
.palette .hint { font-size: 12px; color: var(--muted); margin-top: 8px; }

.b-line {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 8px;
}
.b-line.dragover { border-color: var(--accent); border-style: dashed; }
.b-line .drag { cursor: grab; color: var(--muted); padding-top: 2px; }
.b-parts { flex: 1; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.b-part-text {
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 2px 6px;
  min-width: 40px;
  outline: none;
}
.b-part-text:hover, .b-part-text:focus { border-color: var(--border); background: #fbfcfc; }
.b-part-blk {
  border: 1px solid var(--accent2);
  color: var(--accent2);
  background: #fdf2f0;
  border-radius: 8px;
  padding: 2px 10px;
  font-size: 13px;
  cursor: pointer;
}
.b-line-tools { display: flex; gap: 2px; }

.section-head {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 18px 0 10px;
}
.section-head input {
  font-weight: 700;
  font-size: 16px;
  border: none;
  background: none;
  border-bottom: 1px dashed var(--border);
  padding: 2px 4px;
  flex: 1;
  outline: none;
}
.ai-guidance input {
  width: 100%;
  border: 1px dashed var(--accent2);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 13px;
  color: var(--accent2);
  background: #fefaf9;
  margin-bottom: 10px;
}

.opt-editor-row { display: flex; gap: 6px; margin-bottom: 6px; align-items: center; }
.opt-editor-row input { flex: 1; border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; }

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  border-radius: 999px;
  padding: 10px 22px;
  box-shadow: var(--shadow);
  z-index: 99;
  opacity: 0;
  transition: opacity .2s;
  pointer-events: none;
}
.toast.show { opacity: 1; }

/* ---------- narrow screens (browser pane, phones) ---------- */
@media (max-width: 640px) {
  .topbar { flex-direction: column; align-items: stretch; gap: 8px; padding: 10px 12px; }
  .brand { justify-content: center; }
  .topnav { justify-content: center; }
  .topnav button { flex: 1 1 auto; padding: 6px 8px; font-size: 14px; text-align: center; }

  main { padding: 16px 10px 60px; }

  .view-title h1 { font-size: 20px; }
  .view-title > div { width: 100%; }
  .view-title .btn { flex: 1 1 auto; text-align: center; }

  .modal { padding: 18px 14px; }
  .note-line { padding: 9px 10px; }
  .editor-bar h1 { min-width: 0; }

  /* FDI chart: split each arch into two rows of 8 (right side, then left) */
  .tooth-grid { grid-template-columns: repeat(8, 1fr); }
  .tooth { font-size: 12px; padding: 8px 0; }
}

/* touch devices have no hover — keep per-line tools visible */
@media (hover: none) {
  .nl-tools { opacity: 1; }
}
