:root {
  --cockpit: #111d28;
  --cockpit-soft: #1c2d3a;
  --board: #ebe7dc;
  --playfield: #172733;
  --playfield-raised: #1d303e;
  --playfield-line: #344854;
  --ink: #22313c;
  --muted: #7e8d95;
  --signal: #ffbd29;
  --red: #ee5547;
  --yellow: #f4c64e;
  --green: #43ad79;
  --blue: #4597e6;
  --purple: #a27cdb;
  --orange: #f08a45;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
}

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

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--cockpit);
}

body {
  min-width: 320px;
  color: var(--board);
  background:
    radial-gradient(circle at 50% -15%, rgba(69, 151, 230, 0.13), transparent 42%),
    var(--cockpit);
  font-family: "SF Pro Text", "PingFang SC", "Microsoft YaHei", sans-serif;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
}

button {
  color: inherit;
  font: inherit;
}

button:focus-visible {
  outline: 3px solid var(--signal);
  outline-offset: 2px;
}

.app-shell {
  width: min(100%, 1040px);
  height: 100vh;
  height: 100svh;
  height: 100dvh;
  margin: 0 auto;
  padding:
    calc(6px + var(--safe-top))
    calc(10px + var(--safe-right))
    calc(8px + var(--safe-bottom))
    calc(10px + var(--safe-left));
  display: grid;
  grid-template-rows: 46px minmax(0, 1fr);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid rgba(235, 231, 220, 0.14);
}

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

.brand h1,
.status-strip p,
.rules-dialog h2,
.rules-dialog p {
  margin: 0;
}

.brand h1 {
  font-family: "Songti SC", "STSong", serif;
  font-size: 21px;
  line-height: 1;
  letter-spacing: 0.12em;
}

.brand-mark {
  width: 30px;
  height: 30px;
  padding: 6px;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 3px;
  border: 1px solid rgba(235, 231, 220, 0.34);
  border-radius: 50%;
  transform: rotate(45deg);
}

.brand-mark i {
  border-radius: 50%;
  background: var(--signal);
}

.brand-mark i:nth-child(2) { background: var(--blue); }
.brand-mark i:nth-child(3) { background: var(--red); }
.brand-mark i:nth-child(4) { background: var(--green); }

.header-actions {
  display: flex;
  align-items: center;
  gap: 7px;
}

.attempt-gauge {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border: 1px solid rgba(255, 189, 41, 0.55);
  border-radius: 50%;
  color: var(--signal);
  background: rgba(255, 189, 41, 0.06);
  font-family: "SF Mono", "Roboto Mono", monospace;
  font-size: 14px;
  font-weight: 700;
}

.connection-dot {
  width: 10px;
  height: 34px;
  display: grid;
  place-items: center;
}

.connection-dot i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 0 3px rgba(67, 173, 121, 0.12);
}

.connection-dot.offline i { background: var(--signal); }

.icon-button {
  width: 34px;
  height: 34px;
  padding: 0;
  border: 1px solid rgba(235, 231, 220, 0.2);
  border-radius: 8px;
  color: var(--board);
  background: rgba(235, 231, 220, 0.04);
  font-family: "SF Mono", "Roboto Mono", monospace;
  font-size: 14px;
  cursor: pointer;
}

.new-button {
  border-color: rgba(255, 189, 41, 0.5);
  color: var(--signal);
  background: rgba(255, 189, 41, 0.1);
  font-size: 20px;
  font-weight: 800;
}

main {
  min-height: 0;
  display: grid;
  grid-template-rows: 30px minmax(0, 1fr);
}

.status-strip {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 4px;
}

.status-strip > p {
  color: #aeb9be;
  font-size: 11px;
  font-weight: 650;
  letter-spacing: 0.08em;
}

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

.feedback-key > div {
  display: flex;
  align-items: center;
  gap: 5px;
  color: #7e8d95;
  font-family: "SF Mono", "Roboto Mono", monospace;
  font-size: 9px;
}

.game-layout {
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border: 1px solid rgba(235, 231, 220, 0.15);
  border-radius: 12px;
  background: var(--cockpit-soft);
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.24);
}

.board-frame {
  position: relative;
  min-height: 0;
  flex: 1 1 auto;
  padding: 5px 10px;
  overflow: hidden;
  color: var(--board);
  background: var(--playfield);
}

.board {
  height: 100%;
  display: flex;
  flex-direction: column-reverse;
}

.board-row {
  position: relative;
  min-height: 0;
  flex: 1 1 0;
  display: grid;
  grid-template-columns: 23px minmax(0, 1fr) 40px;
  align-items: center;
  gap: 7px;
  border-bottom: 1px solid var(--playfield-line);
}

.board-row:first-child { border-bottom: 0; }

.board-row.active {
  background: linear-gradient(90deg, rgba(255, 189, 41, 0.13), rgba(255, 189, 41, 0.02) 72%);
}

.board-row.active::before {
  content: "";
  position: absolute;
  left: -10px;
  top: 16%;
  bottom: 16%;
  width: 3px;
  border-radius: 0 3px 3px 0;
  background: var(--signal);
}

.row-number {
  color: #7f929d;
  font-family: "SF Mono", "Roboto Mono", monospace;
  font-size: 9px;
  font-weight: 700;
}

.peg-list {
  width: min(100%, 230px);
  display: flex;
  align-items: center;
  justify-content: space-around;
}

.peg,
.palette-peg {
  --peg-base: #8a98a4;
  position: relative;
  flex: 0 0 auto;
  border-radius: 50%;
  background: var(--peg-base);
}

.peg {
  border: 1px solid rgba(255, 255, 255, 0.14);
  box-shadow:
    inset 0 5px 8px rgba(255, 255, 255, 0.2),
    inset 0 -7px 11px rgba(0, 0, 0, 0.31),
    0 3px 6px rgba(0, 0, 0, 0.3);
}

.palette-peg {
  border: 0;
  box-shadow:
    inset 0 8px 12px rgba(255, 255, 255, 0.2),
    inset 0 -10px 16px rgba(0, 0, 0, 0.3),
    0 5px 9px rgba(0, 0, 0, 0.28);
}

.peg::after,
.palette-peg::after {
  content: "";
  position: absolute;
  left: 21%;
  top: 16%;
  width: 24%;
  height: 16%;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.55);
  transform: rotate(-18deg);
}

.peg::after {
  background: rgba(255, 255, 255, 0.46);
}

.peg,
.empty-peg {
  width: clamp(22px, 7vw, 29px);
  height: clamp(22px, 7vw, 29px);
}

.empty-peg {
  flex: 0 0 auto;
  border: 1px dashed rgba(235, 231, 220, 0.22);
  border-radius: 50%;
}

.feedback {
  display: grid;
  grid-template-columns: repeat(2, 10px);
  justify-content: end;
  place-items: center;
  gap: 3px;
}

.feedback-pin,
.key-pin {
  width: 9px;
  height: 9px;
  border: 1.5px solid rgba(235, 231, 220, 0.2);
  border-radius: 50%;
}

.feedback-pin.exact,
.key-pin.exact {
  border-color: var(--board);
  background: var(--board);
  box-shadow: 0 0 0 2px rgba(235, 231, 220, 0.08);
}

.feedback-pin.color-only,
.key-pin.color-only {
  border: 2px solid var(--board);
  background: transparent;
}

.feedback-pin.empty {
  width: 5px;
  height: 5px;
  border: 0;
  background: rgba(235, 231, 220, 0.18);
}

.control-deck {
  flex: 0 0 120px;
  padding: 8px;
  display: grid;
  grid-template-rows: 46px 54px;
  gap: 8px;
  border-top: 1px solid rgba(235, 231, 220, 0.12);
  background: var(--cockpit-soft);
}

.palette {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 5px;
}

.palette-button {
  min-width: 0;
  min-height: 46px;
  padding: 3px;
  display: grid;
  place-items: center;
  border: 1px solid transparent;
  border-radius: 9px;
  color: #aeb9be;
  background: rgba(7, 16, 23, 0.24);
  cursor: pointer;
}

.palette-button span:last-child { display: none; }

.palette-button:active {
  transform: translateY(1px) scale(0.97);
  background: rgba(7, 16, 23, 0.46);
}

.palette-button:disabled {
  opacity: 0.42;
  cursor: not-allowed;
}

.palette-peg {
  width: 34px;
  height: 34px;
}

.color-red { --peg-base: #ee5547; }
.color-yellow { --peg-base: #f4c64e; }
.color-green { --peg-base: #43ad79; }
.color-blue { --peg-base: #4597e6; }
.color-purple { --peg-base: #a27cdb; }
.color-orange { --peg-base: #f08a45; }

.control-actions {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) minmax(0, 2fr);
  gap: 8px;
}

.rollback-button,
.submit-button,
.dialog-primary {
  min-height: 54px;
  border: 0;
  border-radius: 9px;
  cursor: pointer;
}

.rollback-button {
  color: var(--board);
  background: rgba(235, 231, 220, 0.11);
  font-family: "SF Mono", "Roboto Mono", monospace;
  font-size: 24px;
  font-weight: 700;
}

.rollback-button:disabled {
  color: #53636c;
  background: rgba(235, 231, 220, 0.045);
  cursor: not-allowed;
}

.clear-button { font-size: 22px; }

.submit-button {
  padding: 0 17px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: var(--ink);
  background: var(--signal);
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0.08em;
  box-shadow: 0 7px 20px rgba(255, 189, 41, 0.12);
}

.submit-button:disabled {
  color: #62727b;
  background: #2b414f;
  box-shadow: none;
  cursor: not-allowed;
}

.submit-icon { font-size: 18px; }

.solution {
  position: absolute;
  z-index: 2;
  left: 10px;
  right: 10px;
  bottom: 7px;
  min-height: 44px;
  padding: 7px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-radius: 9px;
  color: var(--board);
  border: 1px solid rgba(255, 189, 41, 0.25);
  background: rgba(7, 16, 23, 0.97);
  box-shadow: 0 10px 28px rgba(17, 29, 40, 0.3);
}

.solution[hidden] { display: none; }

.solution > span {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.12em;
}

.solution-pegs {
  display: flex;
  gap: 6px;
}

.solution-pegs .peg {
  width: 27px;
  height: 27px;
}

.eyebrow {
  color: var(--signal);
  font-family: "SF Mono", "Roboto Mono", monospace;
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.18em;
}

.rules-dialog {
  width: min(460px, calc(100% - 24px));
  max-height: calc(100dvh - 24px);
  padding: 0;
  overflow: auto;
  border: 0;
  border-radius: 13px;
  color: var(--board);
  background: transparent;
}

.rules-dialog::backdrop {
  background: rgba(4, 10, 15, 0.82);
  backdrop-filter: blur(5px);
}

.dialog-card {
  position: relative;
  padding: 28px;
  background: var(--playfield-raised);
  box-shadow: 0 28px 80px rgba(0, 0, 0, 0.4);
}

.dialog-close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 36px;
  height: 36px;
  border: 0;
  color: var(--board);
  background: transparent;
  font-size: 25px;
  cursor: pointer;
}

.rules-dialog h2 {
  margin-top: 6px;
  font-family: "Songti SC", "STSong", serif;
  font-size: 27px;
}

.rules-dialog ol {
  margin: 21px 0 0;
  padding: 0;
  list-style: none;
  counter-reset: rules;
}

.rules-dialog li {
  position: relative;
  min-height: 45px;
  padding: 0 0 15px 39px;
  counter-increment: rules;
}

.rules-dialog li::before {
  content: "0" counter(rules);
  position: absolute;
  left: 0;
  top: 1px;
  color: #849096;
  font-family: "SF Mono", monospace;
  font-size: 10px;
}

.rules-dialog li::after {
  content: "";
  position: absolute;
  left: 12px;
  top: 18px;
  bottom: 4px;
  border-left: 1px solid rgba(235, 231, 220, 0.16);
}

.rules-dialog li:last-child::after { display: none; }
.rules-dialog li b,
.rules-dialog li span { display: block; }
.rules-dialog li b { font-size: 13px; }
.rules-dialog li span { margin-top: 2px; color: #9aabb3; font-size: 11px; line-height: 1.45; }

.dialog-primary {
  width: 100%;
  margin-top: 4px;
  color: var(--ink);
  background: var(--signal);
  font-size: 13px;
  font-weight: 800;
}

.toast {
  position: fixed;
  z-index: 10;
  left: 50%;
  bottom: calc(122px + var(--safe-bottom));
  max-width: calc(100% - 32px);
  padding: 9px 13px;
  border-radius: 8px;
  border: 1px solid rgba(235, 231, 220, 0.12);
  color: var(--board);
  background: #263a48;
  font-size: 11px;
  font-weight: 700;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.32);
  opacity: 0;
  transform: translate(-50%, 10px);
  pointer-events: none;
  transition: opacity 160ms ease, transform 160ms ease;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

@media (min-width: 761px) {
  body {
    display: grid;
    place-items: center;
  }

  .app-shell {
    padding:
      calc(12px + var(--safe-top))
      calc(20px + var(--safe-right))
      calc(14px + var(--safe-bottom))
      calc(20px + var(--safe-left));
    grid-template-rows: 52px minmax(0, 1fr);
  }

  main { grid-template-rows: 34px minmax(0, 1fr); }
  .brand-mark { width: 34px; height: 34px; }
  .brand h1 { font-size: 23px; }
  .game-layout { display: grid; grid-template-columns: minmax(0, 1.65fr) minmax(260px, 0.65fr); }
  .board-frame { padding: 8px 18px; }
  .board-row { grid-template-columns: 28px minmax(0, 1fr) 48px; gap: 12px; }
  .board-row.active::before { left: -18px; }
  .peg-list { width: min(100%, 320px); }
  .peg,
  .empty-peg { width: clamp(25px, 4vh, 34px); height: clamp(25px, 4vh, 34px); }
  .control-deck {
    min-width: 0;
    padding: 18px;
    display: flex;
    flex-direction: column;
    gap: 14px;
    border-top: 0;
    border-left: 1px solid rgba(235, 231, 220, 0.12);
  }
  .palette { grid-template-columns: repeat(3, 1fr); gap: 12px; }
  .palette-button { min-height: 76px; }
  .palette-button span:last-child { display: block; font-size: 9px; }
  .palette-peg { width: 38px; height: 38px; }
  .control-actions { margin-top: auto; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) minmax(0, 2fr); }
  .rollback-button,
  .submit-button { min-height: 54px; }
  .toast { bottom: calc(26px + var(--safe-bottom)); }
}

@media (max-width: 760px) {
  .app-shell {
    position: fixed;
    inset: 0;
    width: 100%;
    height: auto;
    min-height: 0;
  }
}

@media (max-height: 590px) and (max-width: 760px) {
  .app-shell { grid-template-rows: 42px minmax(0, 1fr); padding-top: calc(3px + var(--safe-top)); padding-bottom: calc(4px + var(--safe-bottom)); }
  main { grid-template-rows: 25px minmax(0, 1fr); }
  .brand-mark { width: 27px; height: 27px; padding: 5px; }
  .brand h1 { font-size: 18px; }
  .attempt-gauge,
  .icon-button,
  .connection-dot { height: 30px; }
  .attempt-gauge,
  .icon-button { width: 30px; }
  .status-strip > p { font-size: 10px; }
  .control-deck { flex-basis: 106px; grid-template-rows: 40px 48px; gap: 6px; padding: 6px; }
  .palette-button { min-height: 40px; }
  .palette-peg { width: 30px; height: 30px; }
  .rollback-button,
  .submit-button { min-height: 43px; }
  .peg,
  .empty-peg { width: 22px; height: 22px; }
  .toast { bottom: calc(106px + var(--safe-bottom)); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0.01ms !important; }
}
