:root {
  color-scheme: light;
  --bg: #f6f3ee;
  --surface: #fffdf9;
  --raised: #ffffff;
  --ink: #2a2723;
  --muted: #6f6a62;
  --line: #e2dcd2;
  --accent: #9a3b26;
  --accent-ink: #ffffff;
  --danger: #c0392b;
  --mute-mark: #d33;
  --ghost: rgba(42, 39, 35, 0.18);
  --shadow: 0 1px 2px rgba(40, 30, 20, 0.06), 0 4px 14px rgba(40, 30, 20, 0.06);
  --radius: 14px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #1c1b19;
    --surface: #252320;
    --raised: #2d2b27;
    --ink: #ece7df;
    --muted: #a39d93;
    --line: #3b3733;
    --accent: #e6876b;
    --accent-ink: #1c1b19;
    --danger: #f07167;
    --mute-mark: #f07167;
    --ghost: rgba(236, 231, 223, 0.22);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  }
}

* { box-sizing: border-box; }

/* Elements styled with a display value would otherwise ignore `hidden`. */
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--ink);
  font-size: 16px;
  line-height: 1.45;
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

button, input, textarea { font: inherit; color: inherit; }

button {
  cursor: pointer;
  border: 1px solid var(--line);
  background: var(--raised);
  border-radius: 10px;
  padding: 0.55rem 0.95rem;
  min-height: 44px;
}
button:hover { border-color: var(--muted); }
button:focus-visible, summary:focus-visible, input:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}
button.plain { border: none; background: none; padding-inline: 0.4rem; }
button.strong { color: var(--accent); font-weight: 600; }
button.danger { color: var(--danger); }
button.danger-fill { background: var(--danger); border-color: var(--danger); color: #fff; font-weight: 600; }

/* Top bar */

.bar {
  position: sticky;
  top: 0;
  z-index: 5;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(12px);
  -webkit-backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 1px solid var(--line);
  padding: calc(env(safe-area-inset-top, 0px) + 0.6rem) max(1rem, env(safe-area-inset-right, 0px)) 0.75rem max(1rem, env(safe-area-inset-left, 0px));
}
.bar-row, .bar input { max-width: 960px; margin-inline: auto; }
.bar-row { display: flex; align-items: center; gap: 0.6rem; }
.bar h1 { margin: 0; font-size: 1.35rem; letter-spacing: -0.01em; }
.count { color: var(--muted); font-size: 0.9rem; }
.bar h1, .count, .bar-actions button { white-space: nowrap; }
@media (max-width: 480px) {
  .bar h1 { font-size: 1.2rem; }
  .count { display: none; }
}
.bar-actions { margin-left: auto; display: flex; align-items: center; gap: 0.35rem; }
.bar input {
  display: block;
  width: 100%;
  margin-top: 0.6rem;
  padding: 0.6rem 0.85rem;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--surface);
}

.menu { position: relative; }
.menu summary {
  list-style: none;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 10px;
  cursor: pointer;
  fill: var(--ink);
}
.menu summary::-webkit-details-marker { display: none; }
.menu[open] summary { background: var(--line); }
.menu-list {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  width: min(290px, calc(100vw - 2rem));
  background: var(--raised);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow), 0 12px 32px rgba(0, 0, 0, 0.12);
  padding: 0.35rem;
  display: grid;
}
.menu-list button {
  border: none;
  background: none;
  text-align: left;
  display: grid;
  border-radius: 8px;
}
.menu-list button:hover { background: var(--bg); }
.menu-list small { color: var(--muted); font-size: 0.8rem; }

/* Chord grid */

main { max-width: 960px; margin: 0 auto; padding: 1rem max(1rem, env(safe-area-inset-left, 0px)) 2rem; min-height: calc(100dvh - 9rem); }

.grid {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 0.8rem;
}
.card {
  display: block;
  width: 100%;
  padding: 0.4rem;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  transition: transform 0.12s ease;
}
.card:hover { transform: translateY(-2px); border-color: var(--line); }
.card svg, .diagram svg { display: block; width: 100%; height: auto; }

.empty { text-align: center; color: var(--muted); padding: 3rem 1rem; max-width: 26rem; margin: 0 auto; }
.empty-title { color: var(--ink); font-size: 1.15rem; font-weight: 600; margin-bottom: 0.25rem; }

/* Sheets */

dialog { color: var(--ink); }
dialog::backdrop { background: rgba(20, 16, 12, 0.45); }

.sheet {
  width: min(480px, calc(100% - 2rem));
  max-height: calc(100dvh - 2rem);
  padding: 0;
  border: none;
  border-radius: 18px;
  background: var(--surface);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  overflow: hidden;
}
.sheet[open] { display: flex; flex-direction: column; }
.sheet > form { display: flex; flex-direction: column; min-height: 0; flex: 1; }

@media (max-width: 560px) {
  .sheet {
    width: 100%;
    max-width: 100%;
    height: 100dvh;
    max-height: 100dvh;
    margin: 0;
    border-radius: 0;
  }
}

.sheet-bar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 0.5rem;
  padding: calc(env(safe-area-inset-top, 0px) + 0.35rem) 0.6rem 0.35rem;
  border-bottom: 1px solid var(--line);
}
.sheet-bar h2 { margin: 0; font-size: 1.05rem; text-align: center; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sheet-bar > :first-child { justify-self: start; }
.sheet-bar > :last-child { justify-self: end; }

.sheet-body {
  overflow-y: auto;
  padding: 1rem 1.1rem calc(env(safe-area-inset-bottom, 0px) + 1.25rem);
  display: grid;
  gap: 0.9rem;
}

.diagram {
  background: #fff;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  padding: 0.5rem;
  max-width: 300px;
  width: 100%;
  margin: 0 auto;
}

.shape-line { margin: 0; text-align: center; color: var(--muted); font-size: 0.9rem; }
.shape-line code { color: var(--ink); font-size: 1rem; letter-spacing: 0.08em; }

.actions { display: flex; gap: 0.6rem; justify-content: center; flex-wrap: wrap; }

/* Editor */

.field { display: grid; gap: 0.3rem; }
.field span { font-size: 0.85rem; font-weight: 600; color: var(--muted); }
.field input, .field textarea {
  width: 100%;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--raised);
  resize: vertical;
}
.field input { font-size: 1.15rem; font-weight: 600; }

.board-wrap { display: flex; justify-content: center; }
.board { width: 100%; max-width: 340px; height: auto; touch-action: manipulation; user-select: none; -webkit-user-select: none; }
.board .fret { stroke: var(--muted); stroke-width: 1.5; }
.board .string { stroke: var(--muted); }
.board .nut { fill: var(--ink); }
.board .inlay { fill: var(--line); }
.board .label { fill: var(--muted); font-size: 13px; text-anchor: middle; dominant-baseline: central; }
.board .open { fill: none; stroke: var(--ink); stroke-width: 2; }
.board .muted { stroke: var(--mute-mark); stroke-width: 2.5; stroke-linecap: round; }
.board .dot { fill: var(--ink); }
.board .finger { fill: var(--surface); font-size: 15px; font-weight: 700; text-anchor: middle; dominant-baseline: central; }
.board .cell { cursor: pointer; outline: none; }
.board .cell rect { fill: transparent; }
.board .cell .ghost { fill: var(--ghost); opacity: 0; transition: opacity 0.1s; }
@media (hover: hover) { .board .cell:hover .ghost { opacity: 1; } }
.board .cell:focus-visible rect { stroke: var(--accent); stroke-width: 2; rx: 8; }
.board .marks { pointer-events: none; }

.tools { border: none; margin: 0; padding: 0; display: grid; gap: 0.35rem; justify-items: center; }
.tools legend { font-size: 0.85rem; color: var(--muted); padding: 0; margin: 0 auto 0.35rem; text-align: center; }
.segmented { display: inline-flex; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 3px; gap: 2px; }
.segmented label { position: relative; }
.segmented input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.segmented span { display: grid; place-items: center; min-width: 44px; min-height: 38px; padding: 0 0.6rem; border-radius: 7px; font-weight: 600; font-size: 0.95rem; }
.segmented input:checked + span { background: var(--raised); box-shadow: var(--shadow); color: var(--accent); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); }

.hint { margin: 0; text-align: center; font-size: 0.85rem; color: var(--muted); }
.error { margin: 0; color: var(--danger); font-weight: 600; min-height: 1.45em; text-align: center; }

/* Confirm */

.alert {
  width: min(340px, calc(100% - 2rem));
  border: none;
  border-radius: 16px;
  background: var(--surface);
  padding: 1.25rem 1.25rem 1rem;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
.alert p { margin: 0 0 1rem; }
.alert-actions { display: flex; justify-content: flex-end; gap: 0.5rem; }

/* Toast */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 1rem);
  transform: translateX(-50%);
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  max-width: calc(100% - 2rem);
  padding: 0.35rem 0.5rem 0.35rem 1rem;
  border-radius: 12px;
  background: var(--ink);
  color: var(--bg);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
}
.toast.error { background: var(--danger); color: #fff; }
.toast .plain { color: inherit; text-decoration: underline; }
.toast:not(:has(button:not([hidden]))) { padding-block: 0.7rem; }

/* Credit */

.credit {
  text-align: center;
  font-size: 0.75rem;
  letter-spacing: 0.02em;
  color: var(--muted);
  opacity: 0.7;
  padding: 0 1rem calc(env(safe-area-inset-bottom, 0px) + 1.25rem);
}
.credit a { color: inherit; text-underline-offset: 2px; }
.credit a:hover { color: var(--accent); }
