:root {
  --bg-a: #f7f1de;
  --bg-b: #e7efe2;
  --ink: #1f2a21;
  --muted: #5a6a5f;
  --card: #fff9e8;
  --card-edge: #d8cba5;
  --accent: #89b67a;
  --accent-dark: #4f7a49;
  --warn: #d49a5a;
  --seam: #f08f61;
  --hit: #9dd38a;
  --auto: #9ec2dd;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  font-family: "Nunito", sans-serif;
  background:
    radial-gradient(circle at 10% 20%, #fff6d9 0%, transparent 30%),
    radial-gradient(circle at 90% 80%, #dcebd9 0%, transparent 28%),
    linear-gradient(145deg, var(--bg-a), var(--bg-b));
}

.app-shell {
  width: min(1200px, 100% - 1.6rem);
  margin: 1rem auto 2rem;
  display: grid;
  grid-template-columns: 340px 1fr;
  gap: 1rem;
}

.topbar {
  grid-column: 1 / -1;
  border: 3px solid #202a22;
  border-radius: 14px;
  padding: 1rem;
  background: rgba(255, 250, 236, 0.88);
  box-shadow: 0 6px 0 #c9b98f;
}

h1 {
  margin: 0;
  line-height: 1.1;
  font-family: "Press Start 2P", monospace;
  font-size: clamp(1rem, 2.2vw, 1.35rem);
  color: #2e5036;
}

.topbar p {
  margin: 0.65rem 0 0;
  color: var(--muted);
  font-weight: 700;
}

.title-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.8rem;
}

.title-row > div {
  min-width: 0;
}

.help-launch {
  width: 2rem;
  height: 2rem;
  border: 2px solid #6c726d;
  border-radius: 8px;
  padding: 0;
  line-height: 1;
  font-size: 1.05rem;
  font-weight: 900;
  color: #444a46;
  background: linear-gradient(180deg, #f0f2f1, #d9dddb);
  box-shadow: 0 2px 0 #a7afaa;
}

.help-launch:hover {
  filter: brightness(1.02);
}

.controls {
  border: 3px solid #273326;
  border-radius: 14px;
  background: rgba(255, 252, 244, 0.94);
  box-shadow: 0 6px 0 #ccb994;
  padding: 0.9rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

label {
  display: block;
  margin-bottom: 0.35rem;
  font-weight: 800;
  letter-spacing: 0.03em;
}

.input-row {
  display: flex;
  gap: 0.5rem;
}

input,
button {
  border: 2px solid #2a3528;
  border-radius: 10px;
  font: inherit;
}

input {
  flex: 1;
  min-width: 0;
  padding: 0.65rem 0.7rem;
  background: #fdf7e8;
}

button {
  padding: 0.62rem 0.8rem;
  font-weight: 800;
  cursor: pointer;
  background: linear-gradient(180deg, #b7db9f, #90bc7b);
  color: #183119;
  box-shadow: 0 2px 0 #628555;
}

button:hover {
  filter: brightness(1.03);
}

button:active {
  transform: translateY(1px);
}

button:focus-visible,
input:focus-visible {
  outline: 3px solid #2e7ad0;
  outline-offset: 1px;
}

.stats {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.5rem;
}

.stats p {
  margin: 0;
  border: 2px dashed #bfae87;
  border-radius: 10px;
  padding: 0.38rem 0.48rem;
  background: #fff9ea;
}

.stats span {
  display: block;
  font-size: 0.75rem;
  color: #6a7159;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.stats strong {
  display: block;
  font-size: 1.08rem;
}

.share-launch {
  width: 100%;
  margin-top: 0.25rem;
  border-color: #2c5d3d;
  background: linear-gradient(180deg, #c6ebbb, #9fd48b);
  color: #17351f;
}

.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.45rem;
}

.actions #status {
  margin: 0;
  grid-column: 1 / -1;
  font-size: 0.9rem;
  color: #455142;
  min-height: 1.4em;
}

.legend {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.45rem;
  color: #4d574f;
  font-size: 0.84rem;
}

.swatch {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 3px;
  border: 2px solid #2b3427;
  vertical-align: middle;
}

.swatch.hit {
  background: var(--hit);
}

.swatch.seam {
  background: var(--seam);
}

.swatch.auto {
  background: var(--auto);
}

#history {
  margin: 0;
  padding-left: 1.1rem;
  max-height: 28vh;
  overflow: auto;
  color: #415049;
  font-size: 0.88rem;
}

#history .auto-note {
  color: #446f94;
}

.partitions {
  margin: 0;
  padding: 0;
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.partitions li {
  border: 2px solid #bda675;
  background: #fff4d8;
  border-radius: 999px;
  padding: 0.2rem 0.55rem;
  font-size: 0.75rem;
  color: #5a4a2c;
  white-space: nowrap;
}

.board-wrap {
  border: 3px solid #2b352a;
  border-radius: 14px;
  padding: 0.8rem;
  background: linear-gradient(180deg, #f8efd7, #efe4c4);
  box-shadow: 0 6px 0 #c8b282;
}

.board-stage {
  position: relative;
}

.board {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  grid-template-rows: repeat(4, minmax(0, 1fr));
  gap: 0.55rem;
}

.board-overlay {
  position: absolute;
  inset: 0;
  pointer-events: auto;
}

.card {
  position: relative;
  aspect-ratio: 5 / 3;
  border: 2px solid var(--card-edge);
  border-radius: 8px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.45), transparent),
    var(--card);
  box-shadow: inset 0 0 0 1px rgba(130, 112, 76, 0.14);
  overflow: hidden;
}

.card::before {
  content: "";
  position: absolute;
  inset: 0;
  background-image: repeating-linear-gradient(
    0deg,
    rgba(75, 56, 26, 0.03) 0,
    rgba(75, 56, 26, 0.03) 1px,
    transparent 1px,
    transparent 4px
  );
  pointer-events: none;
}

.card-word {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 90%;
  text-align: center;
  transform: translate(-50%, -50%) rotate(var(--tilt, -2deg));
  font-weight: 800;
  line-height: 1;
  color: #1f2a21;
  text-wrap: balance;
  font-size: var(--word-size, 1rem);
  opacity: 0;
}

.card.revealed {
  border-color: #9ab29d;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.52), transparent),
    #f7fbf5;
}

.card.hit {
  border-color: #84aa7c;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.52), transparent),
    #f0f9ea;
}

.card.revealed .card-word {
  opacity: 1;
}

.card.hinted .card-word {
  opacity: 1;
}

.card.auto {
  border-color: #8aa9c0;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.52), transparent),
    #f0f6fb;
}

.card.hit-flash {
  animation: pop 380ms ease;
}

.card.auto-pivot-flash {
  animation: auto-glow 760ms ease;
}

.seam-marker {
  position: absolute;
  top: 0;
  width: 4px;
  height: 10%;
  border-radius: 8px;
  background: linear-gradient(180deg, #f9b993, #e7784f);
  box-shadow: 0 0 0 1px rgba(98, 46, 25, 0.35);
  pointer-events: auto;
  transform: translateX(-50%);
  z-index: 5;
  cursor: help;
}

.seam-marker:hover,
.seam-marker:focus-visible {
  width: 6px;
  transform: translateX(-50%);
  box-shadow:
    0 0 0 1px rgba(98, 46, 25, 0.35),
    0 0 0 4px rgba(239, 139, 95, 0.2);
  outline: none;
}

.partition-scan {
  position: absolute;
  border-radius: 10px;
  background: rgba(247, 167, 102, 0.16);
  outline: 1px dashed rgba(133, 74, 44, 0.5);
  pointer-events: none;
}

.partition-head {
  position: absolute;
  top: 12%;
  bottom: 12%;
  left: 0;
  width: 6px;
  border-radius: 5px;
  background: linear-gradient(180deg, #ffd4a6, #f08e63);
  box-shadow: 0 0 0 1px rgba(112, 54, 33, 0.4);
  animation-name: scan-move;
  animation-timing-function: linear;
  animation-fill-mode: forwards;
  pointer-events: none;
}

.seam-word {
  position: absolute;
  z-index: 4;
  font-size: 0.68rem;
  font-weight: 800;
  color: #2f7d4e;
  background: #f5ffef;
  border: 1px solid #89b887;
  border-radius: 999px;
  padding: 0.05rem 0.36rem;
  max-width: 18%;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
  pointer-events: none;
}

.seam-tooltip {
  position: fixed;
  z-index: 50;
  min-width: 188px;
  max-width: 280px;
  border: 2px solid #4f7f54;
  border-radius: 12px;
  background: linear-gradient(160deg, #fbffe8, #efffdb);
  box-shadow: 0 10px 24px rgba(35, 57, 37, 0.28);
  padding: 0.5rem 0.65rem;
  color: #1f4731;
}

.seam-tooltip p {
  margin: 0.15rem 0;
  font-size: 0.82rem;
}

.auto-pivot-badge {
  position: absolute;
  right: 0.5rem;
  top: 0.5rem;
  z-index: 20;
  border: 2px solid #a03434;
  border-radius: 999px;
  background: linear-gradient(180deg, #ffecec, #ffd7d7);
  color: #7f1e1e;
  font-size: 0.74rem;
  font-weight: 800;
  padding: 0.18rem 0.5rem;
  box-shadow: 0 4px 12px rgba(133, 45, 45, 0.24);
  pointer-events: none;
  animation: badge-pop 1050ms ease forwards;
}

.bad-triplet-marker {
  position: absolute;
  height: 0;
  border-top: 3px solid #e7784f;
  box-shadow: 0 0 0 1px rgba(98, 46, 25, 0.2);
  border-radius: 2px;
  z-index: 9;
  pointer-events: none;
  opacity: 0.75;
}

.bad-triplet-notch {
  position: absolute;
  top: -10px;
  width: 0;
  height: 0;
  border-left: 7px solid transparent;
  border-right: 7px solid transparent;
  border-bottom: 9px solid #e7784f;
  transform: translateX(-50%);
}

.share-modal {
  border: 0;
  padding: 0;
  background: transparent;
}

.share-modal::backdrop {
  background: rgba(20, 29, 19, 0.45);
  backdrop-filter: blur(2px);
}

.tutorial-modal {
  border: 0;
  padding: 0;
  background: transparent;
}

.tutorial-modal::backdrop {
  background: rgba(20, 29, 19, 0.45);
  backdrop-filter: blur(2px);
}

.tutorial-card {
  width: min(92vw, 540px);
  border: 3px solid #2b3d2a;
  border-radius: 16px;
  background: linear-gradient(160deg, #fffbe8, #edf9df);
  box-shadow: 0 12px 0 #c8b282;
  padding: 1rem;
}

.tutorial-card h2 {
  margin: 0;
  font-size: 1.15rem;
  color: #24442e;
}

.tutorial-card ul {
  margin: 0.7rem 0 0;
  padding-left: 1.15rem;
  color: #273a2a;
}

.tutorial-card li {
  margin: 0.34rem 0;
  line-height: 1.32;
}

.tutorial-actions {
  margin-top: 0.85rem;
  display: flex;
  justify-content: flex-end;
}

.share-card {
  width: min(92vw, 440px);
  border: 3px solid #2b3d2a;
  border-radius: 16px;
  background: linear-gradient(160deg, #fffbe8, #edf9df);
  box-shadow: 0 12px 0 #c8b282;
  padding: 1rem;
}

.share-card h2 {
  margin: 0;
  font-size: 1.15rem;
  color: #24442e;
}

.share-text {
  display: block;
  width: 100%;
  margin: 0;
  border: 2px dashed #bfae87;
  border-radius: 10px;
  background: #fffdf1;
  color: #203325;
  padding: 0.65rem;
  white-space: pre-wrap;
  font-family: "Nunito", sans-serif;
  font-size: 0.98rem;
  line-height: 1.25;
  resize: none;
}

.share-actions {
  margin-top: 0.75rem;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.45rem;
}

.share-actions button[disabled] {
  opacity: 0.55;
  cursor: not-allowed;
  filter: grayscale(0.15);
}

@keyframes scan-move {
  from {
    left: 0;
  }
  to {
    left: calc(100% - 6px);
  }
}

@keyframes auto-glow {
  0% {
    box-shadow: 0 0 0 0 rgba(98, 151, 205, 0);
    transform: scale(1);
  }
  35% {
    box-shadow: 0 0 0 6px rgba(98, 151, 205, 0.2);
    transform: scale(1.02);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(98, 151, 205, 0);
    transform: scale(1);
  }
}

@keyframes badge-pop {
  0% {
    opacity: 0;
    transform: translateY(6px) scale(0.94);
  }
  22% {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
  75% {
    opacity: 1;
    transform: translateY(-2px) scale(1);
  }
  100% {
    opacity: 0;
    transform: translateY(-10px) scale(0.98);
  }
}

@keyframes pop {
  0% {
    transform: scale(0.95);
  }
  60% {
    transform: scale(1.02);
  }
  100% {
    transform: scale(1);
  }
}

@media (max-width: 920px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .board {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    grid-template-rows: repeat(7, minmax(0, 1fr));
  }

  .controls {
    order: 2;
  }

  .board-wrap {
    order: 1;
  }

}
