:root {
  --ink: #1a1612;
  --ink-soft: #3a3128;
  --paper: #e8dcc8;
  --paper-2: #f3ead8;
  --silk: #2a3340;
  --silk-2: #1c2430;
  --cinnabar: #8b3a2a;
  --cinnabar-soft: #a85a48;
  --gold: #b08d57;
  --gold-dim: #8a6d3f;
  --wood: #c9a06a;
  --mist: rgba(26, 22, 18, 0.06);
  --shadow: 0 18px 40px rgba(20, 14, 8, 0.22);
  --serif: "Songti SC", "STSong", "Noto Serif CJK SC", "Source Han Serif SC",
    "PMingLiU", "SimSun", "Palatino Linotype", "Palatino", "Book Antiqua", Georgia, serif;
  --ui: "Palatino Linotype", Palatino, "Songti SC", "Noto Serif CJK SC", Georgia, serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  font-family: var(--ui);
  color: var(--ink);
  background:
    radial-gradient(1200px 700px at 10% -10%, rgba(176, 141, 87, 0.18), transparent 50%),
    radial-gradient(900px 600px at 110% 10%, rgba(42, 51, 64, 0.16), transparent 46%),
    radial-gradient(800px 500px at 50% 120%, rgba(139, 58, 42, 0.1), transparent 50%),
    linear-gradient(180deg, #d9cbb3 0%, var(--paper) 40%, #d4c4a8 100%);
  overflow: hidden;
}

body::before {
  content: "";
  pointer-events: none;
  position: fixed;
  inset: 0;
  opacity: 0.35;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='140' height='140'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.8' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='140' height='140' filter='url(%23n)' opacity='.35'/%3E%3C/svg%3E");
  mix-blend-mode: multiply;
}

#app {
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr;
  position: relative;
  z-index: 1;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 18px 8px;
  border-bottom: 1px solid rgba(80, 55, 28, 0.18);
  background: linear-gradient(180deg, rgba(243, 234, 216, 0.72), rgba(232, 220, 200, 0.35));
  backdrop-filter: blur(8px);
}

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

.taiji {
  width: 42px;
  height: 42px;
  flex: 0 0 auto;
}

.brand h1 {
  margin: 0;
  font-weight: 600;
  font-size: 1.45rem;
  letter-spacing: 0.12em;
  font-family: var(--serif);
}

.brand p {
  margin: 2px 0 0;
  font-size: 0.78rem;
  letter-spacing: 0.16em;
  color: var(--ink-soft);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

main {
  display: grid;
  grid-template-columns: 210px minmax(0, 1fr) 320px;
  gap: 12px;
  padding: 12px 14px 14px;
  min-height: 0;
  overflow: hidden;
}

.panel {
  background: linear-gradient(180deg, rgba(247, 240, 226, 0.78), rgba(232, 220, 200, 0.7));
  border: 1px solid rgba(80, 55, 28, 0.16);
  border-radius: 14px;
  box-shadow: var(--shadow);
  min-height: 0;
}

.panel-left {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow: auto;
}

.board-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  padding: 8px;
  position: relative;
  background:
    radial-gradient(circle at 50% 50%, rgba(255, 250, 235, 0.28), transparent 62%),
    linear-gradient(180deg, rgba(40, 32, 22, 0.05), rgba(40, 32, 22, 0.02));
}

.board-wrap::before {
  content: "";
  position: absolute;
  inset: 10px;
  border-radius: 50%;
  border: 1px solid rgba(176, 141, 87, 0.18);
  pointer-events: none;
}

#board {
  display: block;
  max-width: 100%;
  touch-action: none;
  border-radius: 6px;
  box-shadow:
    0 20px 40px rgba(40, 22, 8, 0.28),
    0 2px 0 rgba(255, 236, 200, 0.25) inset;
  cursor: crosshair;
}

.panel-master {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background:
    linear-gradient(180deg, rgba(32, 40, 52, 0.94), rgba(22, 28, 36, 0.96));
  color: #efe6d4;
  border-color: rgba(176, 141, 87, 0.28);
}

.master-top {
  padding: 14px 14px 8px;
  display: flex;
  gap: 12px;
  align-items: flex-end;
}

.portrait-wrap {
  position: relative;
  width: 118px;
  height: 150px;
  flex: 0 0 auto;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.35);
  border: 1px solid rgba(176, 141, 87, 0.4);
}

.portrait-wrap img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center 12%;
  transition: opacity 0.45s ease;
}

#master-speak-img {
  opacity: 0;
}

#master-speak-img.show {
  opacity: 1;
}

.portrait-wrap.speaking {
  animation: breath 2.8s ease-in-out infinite;
}

@keyframes breath {
  0%,
  100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-2px);
  }
}

.master-id h2 {
  margin: 0 0 4px;
  font-size: 1.15rem;
  letter-spacing: 0.14em;
  font-weight: 600;
}

.master-id p {
  margin: 0;
  font-size: 0.75rem;
  letter-spacing: 0.12em;
  color: rgba(239, 230, 212, 0.65);
}

#oracle-badge {
  margin-top: 8px;
  display: inline-block;
  padding: 3px 8px;
  border: 1px solid rgba(176, 141, 87, 0.45);
  border-radius: 999px;
  font-size: 0.72rem;
  letter-spacing: 0.06em;
  color: var(--gold);
}

.speech-scroll {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 4px 16px 12px;
}

#master-speech {
  margin: 0;
  white-space: pre-wrap;
  line-height: 1.7;
  font-size: 0.95rem;
  letter-spacing: 0.03em;
}

.master-actions {
  padding: 10px 12px 12px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  border-top: 1px solid rgba(176, 141, 87, 0.18);
}

.wisdom-row {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.82rem;
  letter-spacing: 0.06em;
  color: rgba(239, 230, 212, 0.8);
}

button,
.btn {
  font-family: inherit;
  font-size: 0.84rem;
  letter-spacing: 0.08em;
  border-radius: 999px;
  border: 1px solid rgba(80, 55, 28, 0.28);
  background: linear-gradient(180deg, #f7efe0, #e6d6ba);
  color: var(--ink);
  padding: 7px 12px;
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease, background 0.12s ease;
}

button:hover,
.btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 10px rgba(40, 22, 8, 0.12);
}

button:disabled {
  opacity: 0.45;
  cursor: default;
  transform: none;
  box-shadow: none;
}

.btn-ink {
  background: linear-gradient(180deg, #3a3128, #241e18);
  color: #f3ead8;
  border-color: rgba(0, 0, 0, 0.4);
}

.btn-cinnabar {
  background: linear-gradient(180deg, #9a4634, #6f2c20);
  color: #f8eee4;
  border-color: rgba(70, 20, 12, 0.4);
}

.panel-master button {
  background: linear-gradient(180deg, #3d4a5c, #2a3340);
  color: #efe6d4;
  border-color: rgba(176, 141, 87, 0.35);
}

.panel-master .btn-cinnabar {
  background: linear-gradient(180deg, #9a4634, #6f2c20);
}

.group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.group h3,
.stat-label {
  margin: 0;
  font-size: 0.7rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--gold-dim);
  font-weight: 600;
}

.pills {
  display: flex;
  gap: 6px;
}

.pill {
  flex: 1;
  text-align: center;
  padding: 6px 4px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.28);
  border: 1px solid rgba(80, 55, 28, 0.12);
  font-size: 0.8rem;
}

.pill.active {
  outline: 2px solid var(--cinnabar);
  background: rgba(139, 58, 42, 0.1);
}

.stat-line {
  display: flex;
  justify-content: space-between;
  font-size: 0.88rem;
}

.actions-col {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.actions-col button {
  width: 100%;
}

#score-bar {
  padding: 8px;
  border-radius: 10px;
  background: rgba(139, 58, 42, 0.1);
  border: 1px dashed rgba(139, 58, 42, 0.35);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

#score-bar[hidden],
#tutorial-bar[hidden],
#oracle-badge[hidden],
.modal[hidden],
#result-overlay[hidden],
#hex-detail[hidden] {
  display: none !important;
}

#tutorial-bar {
  position: absolute;
  left: 16px;
  right: 16px;
  bottom: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 12px;
  background: rgba(243, 234, 216, 0.94);
  border: 1px solid rgba(80, 55, 28, 0.18);
  z-index: 4;
}

.hint {
  font-size: 0.78rem;
  color: var(--ink-soft);
  line-height: 1.45;
}

.switch {
  appearance: none;
  width: 36px;
  height: 20px;
  border-radius: 99px;
  background: #6b6256;
  position: relative;
  border: 0;
  cursor: pointer;
}

.switch:checked {
  background: #8b3a2a;
}

.switch::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #f3ead8;
  transition: transform 0.15s ease;
}

.switch:checked::after {
  transform: translateX(16px);
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(20, 14, 8, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
  padding: 18px;
}

.modal-card {
  width: min(720px, 100%);
  max-height: min(86vh, 820px);
  overflow: auto;
  background: var(--paper-2);
  border-radius: 16px;
  padding: 20px 22px;
  box-shadow: var(--shadow);
  border: 1px solid rgba(80, 55, 28, 0.2);
}

.modal-card.wide {
  width: min(880px, 100%);
}

.modal-card h2 {
  margin: 0 0 12px;
  letter-spacing: 0.12em;
  font-weight: 600;
}

.choice-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 6px 0 14px;
}

.choice-row label {
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid rgba(80, 55, 28, 0.22);
  background: rgba(255, 255, 255, 0.35);
  cursor: pointer;
}

.choice-row input {
  margin-right: 6px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 8px;
}

#library-search {
  width: 100%;
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid rgba(80, 55, 28, 0.22);
  background: rgba(255, 255, 255, 0.5);
  font-family: inherit;
  margin-bottom: 12px;
}

#library-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(92px, 1fr));
  gap: 8px;
}

.hex-cell {
  border-radius: 10px;
  padding: 8px 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.hex-cell .glyph {
  font-size: 1.6rem;
  line-height: 1;
}

.hex-cell .num {
  font-size: 0.68rem;
  color: var(--gold-dim);
}

.hex-cell .nm {
  font-size: 0.72rem;
  text-align: center;
}

#hex-detail {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid rgba(80, 55, 28, 0.16);
  line-height: 1.6;
}

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

.hex-head .glyph {
  font-size: 3rem;
  line-height: 1;
}

.hex-head h3 {
  margin: 0;
}

.hex-head p {
  margin: 2px 0 0;
  color: var(--ink-soft);
}

#result-overlay {
  position: absolute;
  inset: 12px;
  background: rgba(243, 234, 216, 0.92);
  border-radius: 12px;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 5;
  overflow: auto;
}

#result-overlay h2 {
  margin: 0;
  letter-spacing: 0.12em;
}

#result-headline {
  margin: 0;
  font-size: 1.2rem;
  color: var(--cinnabar);
}

.score-grid {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.score-row {
  display: grid;
  grid-template-columns: 0.7fr 1fr 1fr 1fr 1.1fr;
  gap: 6px;
  font-size: 0.85rem;
}

#drawer {
  display: none;
}

.board-wrap {
  grid-column: 2;
}

.panel-left {
  grid-column: 1;
}

.panel-master {
  grid-column: 3;
}

body.master-collapsed main {
  grid-template-columns: 210px minmax(0, 1fr) 0px;
}

body.master-collapsed .panel-master {
  opacity: 0;
  pointer-events: none;
  min-width: 0;
  overflow: hidden;
  border: 0;
  padding: 0;
}

#btn-menu,
.hdr-play {
  display: none;
}

@media (max-width: 1040px) {
  main {
    grid-template-columns: minmax(0, 1fr) 280px;
  }
  .board-wrap {
    grid-column: 1;
  }
  .panel-master {
    grid-column: 2;
  }
  .panel-left {
    position: fixed;
    left: 10px;
    top: 58px;
    bottom: 10px;
    width: min(260px, 86vw);
    z-index: 16;
    transform: translateX(calc(-100% - 24px));
    transition: transform 0.2s ease;
  }
  body.drawer-open .panel-left {
    transform: none;
  }
  #btn-menu,
  .hdr-play {
    display: inline-flex;
  }
  #drawer {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(20, 14, 8, 0.4);
    z-index: 15;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.18s ease;
  }
  body.drawer-open #drawer {
    opacity: 1;
    pointer-events: auto;
  }
  body.master-collapsed main {
    grid-template-columns: 1fr;
  }
  body.master-collapsed .panel-master {
    grid-column: 1;
    opacity: 0;
    pointer-events: none;
    height: 0;
    overflow: hidden;
  }
}

@media (max-width: 760px) {
  html,
  body {
    height: auto;
    min-height: 100%;
    min-height: 100dvh;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  body {
    overflow: auto;
  }
  #app {
    height: auto;
    min-height: 100dvh;
    grid-template-rows: auto auto;
  }
  header {
    padding: 6px 8px;
    gap: 6px;
    position: sticky;
    top: 0;
    z-index: 8;
  }
  .taiji {
    width: 28px;
    height: 28px;
  }
  .brand {
    gap: 8px;
  }
  .brand h1 {
    font-size: 0.95rem;
    letter-spacing: 0.06em;
  }
  .brand p {
    display: none;
  }
  .header-actions {
    gap: 5px;
  }
  .header-actions button {
    padding: 6px 9px;
    font-size: 0.75rem;
  }
  #btn-collapse {
    display: none !important;
  }
  main {
    display: flex;
    flex-direction: column;
    grid-template-columns: none;
    grid-template-rows: none;
    height: auto;
    overflow: visible;
    padding: 8px;
    gap: 8px;
  }
  .board-wrap,
  .panel-master {
    grid-column: auto;
  }
  .board-wrap {
    min-height: 0;
    width: 100%;
    padding: 4px;
    flex: 0 0 auto;
  }
  .board-wrap::before {
    display: none;
  }
  .panel-master {
    min-height: 0;
    flex: 1 1 auto;
    max-height: none;
  }
  body.master-collapsed .panel-master {
    display: flex !important;
    opacity: 1;
    pointer-events: auto;
    height: auto;
    min-width: 0;
    overflow: visible;
    border-width: 1px;
    padding: 0;
  }
  .portrait-wrap {
    width: 72px;
    height: 92px;
  }
  .master-top {
    padding: 10px 10px 4px;
    align-items: center;
  }
  .master-id h2 {
    font-size: 1rem;
  }
  .speech-scroll {
    max-height: 7.5rem;
    flex: 0 1 auto;
  }
  #master-speech {
    font-size: 0.88rem;
    line-height: 1.55;
  }
  .master-actions {
    padding: 8px 10px 10px;
  }
  .modal {
    padding: 10px;
    align-items: flex-end;
  }
  .modal-card,
  .modal-card.wide {
    width: 100%;
    max-height: min(88dvh, 820px);
    padding: 16px;
  }
  .score-row {
    grid-template-columns: 1fr 1fr;
  }
}

body.thinking #board {
  cursor: wait;
}
