:root {
  /* 2層構造: 淡い寒色の背景 + 純白の作業面 */
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --ink: #1f2730;
  --muted: #6b7480;
  --faint: #9aa3ad;
  --line: #e7ebf0;
  --line-2: #d8dee6;

  /* アクセントは少し温かい青、1色だけ */
  --accent: #4c8dff;
  --accent-weak: #eaf2ff;
  --accent-ink: #1b4fa6;
  --success: #45a66b;
  --danger: #e35b4f;

  /* 黄色は“ルート/お気に入り”だけの極小ポップ */
  --pop: #ffce4a;

  --radius: 14px;
  --radius-md: 12px;
  --radius-sm: 10px;
  --shadow: 0 1px 2px rgba(20, 28, 40, .05), 0 6px 18px rgba(20, 28, 40, .04);
  --shadow-soft: 0 1px 2px rgba(20, 28, 40, .04);

  --nav-height: 64px;
  --header-height: 60px;
  --maxw: 1120px;

  --svg-bg: #ffffff;
  --svg-stroke: #aeb6bf;
  --svg-text: #1f2730;
  --svg-muted: #9aa3ad;

  font-family: "Noto Sans JP", "Yu Gothic UI", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  color-scheme: light;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--bg);
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background: var(--bg);
  font-weight: 400;
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

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

button,
a {
  -webkit-tap-highlight-color: transparent;
}

a {
  color: inherit;
}

.app-shell {
  width: min(100%, var(--maxw));
  margin: 0 auto;
  padding: 12px 20px calc(var(--nav-height) + env(safe-area-inset-bottom) + 16px);
}

/* ---------- Header ---------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 30;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 16px;
  height: var(--header-height);
  margin-bottom: 18px;
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: blur(8px);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  justify-self: start;
}

.brand-mark {
  display: grid;
  width: 34px;
  height: 34px;
  place-items: center;
  border-radius: 10px;
  background: var(--accent);
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, .08);
}

.brand-mark svg {
  display: block;
  width: 23px;
  height: 23px;
}

.brand-text {
  display: grid;
  line-height: 1.2;
}

.brand-name {
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -.01em;
}

.brand-sub {
  color: var(--muted);
  font-size: 12px;
  font-weight: 500;
}

.header-tabs {
  justify-self: center;
  display: inline-flex;
  gap: 2px;
  padding: 3px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--line);
}

.header-tab {
  display: inline-flex;
  align-items: center;
  height: 34px;
  padding: 0 18px;
  border-radius: 999px;
  color: var(--muted);
  text-decoration: none;
  font-size: 14px;
  font-weight: 600;
  transition: background .15s, color .15s;
}

.header-tab:hover {
  color: var(--ink);
}

.header-tab.active {
  background: var(--accent-weak);
  color: var(--accent-ink);
}

.ghost-button {
  justify-self: end;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  padding: 0 16px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: border-color .15s, color .15s;
}

.ghost-button:hover {
  border-color: var(--line-2);
  color: var(--ink);
}

/* ---------- Screens / panes ---------- */
.main {
  display: block;
}

.screen {
  display: none;
}

.screen.active {
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
}

.pane-input {
  min-width: 0;
}

.pane-head {
  margin-bottom: 16px;
}

.pane-title {
  margin: 0;
  font-size: 19px;
  font-weight: 700;
  letter-spacing: -.01em;
}

.pane-hint {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.6;
}

/* ---------- Inputs ---------- */
.search-box {
  display: grid;
  gap: 6px;
  margin-bottom: 18px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  background: var(--surface);
  box-shadow: var(--shadow-soft);
  transition: border-color .15s, box-shadow .15s;
}

.search-box:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-weak);
}

/* 名前入力は補助なので控えめに */
.search-box.compact {
  margin-top: 4px;
  margin-bottom: 0;
  padding: 9px 12px;
  background: var(--surface-2);
}

.search-box.compact input {
  font-size: 16px;
  font-weight: 600;
}

.search-label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.search-icon {
  position: relative;
  display: inline-block;
  flex: 0 0 auto;
  width: 13px;
  height: 13px;
  border: 1.6px solid currentColor;
  border-radius: 999px;
  font-size: 0;
  opacity: .85;
}

.search-icon::after {
  content: "";
  position: absolute;
  right: -5px;
  bottom: -3px;
  width: 7px;
  height: 1.6px;
  border-radius: 999px;
  background: currentColor;
  transform: rotate(45deg);
  transform-origin: left center;
}

.search-box span,
.row-title,
.control-strip label span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

.search-box input,
.search-box textarea,
.control-strip select {
  width: 100%;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--ink);
  font-size: 19px;
  font-weight: 600;
}

.search-box input::placeholder,
.search-box textarea::placeholder {
  color: var(--faint);
  font-weight: 400;
}

.textarea-box textarea {
  min-height: 120px;
  resize: vertical;
  line-height: 1.6;
}

.chip-section {
  margin-bottom: 18px;
}

.row-title {
  margin-bottom: 8px;
}

.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.root-picker {
  display: grid;
  gap: 8px;
}

.accidental-row .chip {
  min-width: 42px;
}

.chip,
.soft-chip {
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--ink);
  padding: 0 14px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: border-color .15s, background .15s, color .15s;
}

.chip:hover,
.soft-chip:hover {
  border-color: var(--accent);
  color: var(--accent-ink);
}

.chip.active {
  border-color: var(--accent);
  background: var(--accent-weak);
  color: var(--accent-ink);
}

.chip:disabled {
  color: var(--faint);
  background: var(--surface-2);
  border-color: transparent;
  cursor: not-allowed;
  opacity: .45;
}

.soft-chip {
  color: var(--accent-ink);
  background: var(--accent-weak);
  border-color: transparent;
}

.picker-grid {
  display: grid;
  gap: 4px;
}

/* 種類セレクタ: 小見出しで塊化して走査負荷を下げる */
.type-groups {
  display: grid;
  gap: 12px;
}

.mobile-type-picker {
  display: none;
}

.more-types {
  width: 100%;
  height: 36px;
  margin-top: 8px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}

.type-group-label {
  margin-bottom: 7px;
  color: var(--faint);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .04em;
}

/* 結果カードからストックへ追加 */
.stock-add {
  flex: none;
  height: 34px;
  padding: 0 13px;
  border: 1px solid var(--accent);
  border-radius: 999px;
  background: var(--surface);
  color: var(--accent-ink);
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  transition: background .15s, border-color .15s, color .15s;
}

.stock-add:hover {
  background: var(--accent-weak);
}

.stock-add.active {
  border-color: var(--success);
  color: #2f7d54;
  background: #eef9f2;
}

/* ストックのカード（削除ボタン付き） */
.stock-card .dc-head {
  justify-content: space-between;
  align-items: center;
}

.dc-remove {
  flex: none;
  width: 26px;
  height: 26px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
  transition: border-color .15s, color .15s, background .15s;
}

.dc-remove:hover {
  border-color: var(--danger);
  color: var(--danger);
  background: #fdf0ee;
}

/* タブの件数バッジ */
.tab-badge {
  display: inline-grid;
  place-items: center;
  min-width: 18px;
  height: 18px;
  margin-left: 6px;
  padding: 0 5px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  vertical-align: middle;
}

.tab-badge[hidden] {
  display: none;
}

/* 履歴 / 定番: コントロールではなくショートカットなので軽く */
.pane-aux {
  min-width: 0;
}

.history {
  margin-bottom: 0;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.history .row-title {
  color: var(--faint);
}

.pane-aux .chip {
  height: 32px;
  padding: 0 12px;
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
  background: var(--surface-2);
  border-color: transparent;
}

.pane-aux .chip:hover {
  color: var(--accent-ink);
  background: var(--accent-weak);
}

/* ---------- Result cards ---------- */
.result-card,
.finder-card {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.result-card {
  padding: 18px;
}

.result-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.result-name {
  margin: 0;
  font-size: 30px;
  font-weight: 800;
  line-height: 1;
  letter-spacing: -.02em;
}

.result-family {
  margin-top: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
}

.diagram-wrap {
  margin: 10px 0;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  background: var(--svg-bg);
  overflow: hidden;
}

.diagram-wrap svg {
  display: block;
  width: 100%;
  height: auto;
}

.shape-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.shape-tab {
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  padding: 0 13px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: border-color .15s, background .15s, color .15s;
}

.shape-tab:hover {
  border-color: var(--accent);
  color: var(--accent-ink);
}

.shape-tab.active {
  border-color: var(--accent);
  background: var(--accent-weak);
  color: var(--accent-ink);
}

/* 構成音: 重複を排し、ダイアグラムのドット色と対応させる */
.tone-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 16px;
}

.tone {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 36px;
  padding: 0 13px 0 11px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface-2);
}

.tone-dot {
  width: 10px;
  height: 10px;
  border-radius: 999px;
  flex: none;
  background: var(--muted);
}

.tone-dot.degree-R { background: #e85d4f; }
.tone-dot.degree-flat2 { background: #8d6adf; }
.tone-dot.degree-2,
.tone-dot.degree-6,
.tone-dot.degree-9 { background: #4b9fe1; }
.tone-dot.degree-flat3,
.tone-dot.degree-3 { background: #46a77c; }
.tone-dot.degree-4 { background: #d89a00; }
.tone-dot.degree-flat5,
.tone-dot.degree-5 { background: #7a6ee6; }
.tone-dot.degree-sharp5 { background: #b35fb7; }
.tone-dot.degree-flatflat7,
.tone-dot.degree-flat7,
.tone-dot.degree-7 { background: #7b5b35; }
.tone b {
  font-size: 15px;
  font-weight: 700;
}

.tone-role {
  color: var(--muted);
  font-size: 12px;
  font-weight: 500;
}

.note-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(76px, 1fr));
  gap: 8px;
  margin-top: 14px;
}

.note-pill {
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  padding: 9px 10px;
}

.note-pill b {
  display: block;
  font-size: 15px;
  font-weight: 700;
}

.note-pill span {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 500;
}

/* ---------- Key screen ---------- */
.control-strip {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: end;
  margin-bottom: 16px;
}

.control-strip label {
  display: grid;
  gap: 5px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  padding: 9px 12px;
}

.control-strip select {
  font-size: 16px;
}

.segmented {
  display: inline-grid;
  grid-template-columns: 1fr 1fr;
  height: 48px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  padding: 4px;
}

.segment {
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  padding: 0 14px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s, color .15s;
}

.segment.active {
  background: var(--surface);
  color: var(--accent-ink);
  box-shadow: var(--shadow-soft);
}

/* キー画面: 操作行 + 全ダイアトニックの一覧 */
.key-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  gap: 12px;
  max-width: 560px;
}

.key-controls .control-strip {
  flex: 1 1 320px;
  margin-bottom: 0;
}

.chip.toggle {
  height: 48px;
  flex: none;
}

.diatonic-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(232px, 1fr));
  gap: 12px;
  margin-top: 18px;
}

.diatonic-grid.hide-shapes {
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
}

.diatonic-grid.hide-shapes .diagram-wrap {
  display: none;
}

.degree-card {
  display: block;
  text-align: left;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  background: var(--surface);
  padding: 13px 14px;
  cursor: pointer;
  transition: border-color .15s, box-shadow .15s, transform .1s;
}

.degree-card:hover {
  border-color: var(--accent);
  box-shadow: var(--shadow);
}

.degree-card:active {
  transform: translateY(1px);
}

.dc-head {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.dc-head .roman {
  color: var(--accent);
  font-size: 13px;
  font-weight: 700;
}

.dc-head .chord {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -.01em;
}

.dc-tones {
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 500;
  letter-spacing: .04em;
}

.degree-card .diagram-wrap {
  margin: 10px 0 0;
}

/* ---------- Identify screen ---------- */
.finder-results {
  display: grid;
  gap: 12px;
}

.finder-card {
  padding: 16px;
}

.finder-card h2,
.finder-card h3 {
  margin: 0;
  font-size: 22px;
  font-weight: 700;
}

.finder-card p {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.6;
}

.mini-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.link-button {
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--accent-ink);
  padding: 0 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: border-color .15s, background .15s;
}

.link-button:hover {
  border-color: var(--accent);
  background: var(--accent-weak);
}

.empty-state {
  padding: 18px;
  border: 1px dashed var(--line-2);
  border-radius: var(--radius-md);
  color: var(--muted);
  background: var(--surface-2);
  font-size: 13px;
  line-height: 1.7;
}

/* ---------- Bottom nav (mobile only) ---------- */
.bottom-nav {
  position: fixed;
  z-index: 40;
  left: 0;
  right: 0;
  bottom: 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  height: calc(var(--nav-height) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  border-top: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(10px);
}

.nav-item {
  position: relative;
  display: grid;
  place-items: center;
  color: var(--muted);
  text-decoration: none;
  font-size: 13px;
  font-weight: 600;
}

.nav-item .tab-badge {
  position: absolute;
  top: 7px;
  right: calc(50% - 34px);
  margin-left: 0;
}

.nav-item.active {
  color: var(--accent-ink);
}

.nav-item.active b {
  position: relative;
}

.nav-item.active b::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -7px;
  width: 5px;
  height: 5px;
  border-radius: 999px;
  background: var(--accent);
  transform: translateX(-50%);
}

/* ---------- Desktop ---------- */
@media (min-width: 760px) {
  .bottom-nav {
    display: none;
  }

  .app-shell {
    padding-bottom: 40px;
  }

  /* 入力 + 結果は中央寄せで、結果カラムは広がりすぎないよう上限を設ける */
  .screen.active {
    grid-template-columns: 320px minmax(0, 560px);
    column-gap: 32px;
    row-gap: 20px;
    align-items: start;
    justify-content: center;
  }

  /* 結果は常に右カラムで縦に通し、左カラムに入力→履歴を積む */
  .pane-result {
    grid-column: 2;
    grid-row: 1 / 99;
    position: sticky;
    top: calc(var(--header-height) + 18px);
  }

  /* キー画面・ストック画面は一覧を活かすため全幅レイアウト */
  #keyScreen.active,
  #stockScreen.active {
    display: block;
  }

  .result-card {
    padding: 22px;
  }

  .result-name {
    font-size: 34px;
  }
}

/* Hide header tabs on small screens (bottom nav takes over) */
@media (max-width: 759px) {
  .header-tabs {
    display: none;
  }

  .app-header {
    grid-template-columns: 1fr auto;
    height: 52px;
    margin-bottom: 14px;
  }

  .search-box.compact {
    grid-template-columns: auto 1fr;
    align-items: center;
    gap: 10px;
    padding: 7px 11px;
  }

  .search-box.compact input {
    min-width: 0;
    font-size: 15px;
  }

  .root-picker {
    gap: 7px;
  }

  .root-base-row,
  .accidental-row {
    flex-wrap: nowrap;
  }

  .root-base-row .chip {
    flex: 1 1 0;
    min-width: 0;
    padding-inline: 0;
  }

  .accidental-row .chip {
    flex: 0 0 46px;
  }

  .mobile-type-picker {
    display: block;
  }

  .mobile-type-picker .chip-row {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
  }

  .mobile-type-picker .chip {
    padding-inline: 0;
  }

  #typeGroups {
    display: none;
  }

  #typeGroups.expanded {
    display: grid;
    margin-top: 12px;
  }
}

@media (max-width: 420px) {
  .app-shell {
    padding-inline: 16px;
  }

  .brand-sub {
    display: none;
  }

  .control-strip {
    grid-template-columns: 1fr;
  }

  .result-name {
    font-size: 28px;
  }
}
