:root {
  --bg: #f7f8fb;
  --ink: #202536;
  --muted: #697084;
  --line: #d9deea;
  --slot: #ffffff;
  --card: #fffdf8;
  --accent: #3167d6;
  --green: #dff6e6;
  --yellow: #fff4bd;
  --factor: #4a9a68;
  font-family: "Hiragino Sans", "Yu Gothic UI", "Yu Gothic", Meiryo, system-ui, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  margin: 0;
  color: var(--ink);
  background: linear-gradient(180deg, #ffffff 0%, var(--bg) 38%, #edf1f7 100%);
}

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

.app-shell {
  width: min(1040px, calc(100% - 28px));
  margin: 0 auto;
  padding: 22px 0 32px;
}

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 18px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: clamp(1.55rem, 3vw, 2.15rem);
  letter-spacing: 0;
}

.subtitle {
  margin-top: 6px;
  color: var(--muted);
  font-size: 0.96rem;
}

.help-panel {
  max-width: 680px;
  margin-top: 8px;
  color: var(--muted);
  font-size: 0.88rem;
}

.help-panel summary {
  width: max-content;
  cursor: pointer;
  color: var(--accent);
  font-weight: 800;
}

.help-panel ul {
  display: grid;
  gap: 5px;
  margin: 8px 0 0;
  padding-left: 1.2em;
}

.stats {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.stats span,
.zone-head button,
.tweet-link {
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  padding: 7px 12px;
  color: var(--ink);
  font-weight: 700;
  text-decoration: none;
}

.playfield {
  position: relative;
  overflow: hidden;
  scroll-behavior: smooth;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.78);
  padding: clamp(18px, 4vw, 34px);
  box-shadow: 0 16px 42px rgba(36, 48, 82, 0.08);
}

.playfield.menu-open {
  min-height: 250px;
  padding: 8px;
}

.equation.is-waiting {
  opacity: 0.18;
}

.start-panel {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: start center;
  overflow-y: auto;
  padding: 8px;
  background: rgba(255, 255, 255, 0.82);
}

.start-panel-inner {
  width: min(520px, 100%);
  margin: auto 0;
  display: grid;
  gap: 8px;
  justify-items: center;
}

.setting-list {
  width: 100%;
  display: grid;
  gap: 5px;
}

.setting-toggle {
  display: grid;
  grid-template-columns: minmax(4.8em, max-content) minmax(0, 1fr);
  align-items: center;
  gap: 5px;
  margin: 0;
  min-width: 0;
  padding: 4px 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.setting-label {
  color: var(--ink);
  font-size: clamp(0.62rem, 2.6vw, 0.86rem);
  font-weight: 700;
  white-space: nowrap;
}

.switch-control {
  min-width: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0;
  padding: 2px;
  border: 1px solid #cbd4e2;
  border-radius: 999px;
  background: #edf1f7;
}

.switch-control label {
  min-width: 0;
}

.switch-control input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.switch-control span {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 24px;
  padding: 0 3px;
  border-radius: 999px;
  color: var(--muted);
  cursor: pointer;
  font-size: clamp(0.58rem, 2.55vw, 0.84rem);
  font-weight: 700;
  line-height: 1;
  white-space: nowrap;
  transition: background 140ms ease, color 140ms ease, box-shadow 140ms ease;
}

.switch-control input:checked + span {
  background: #fff;
  color: var(--accent);
  box-shadow: 0 2px 8px rgba(36, 48, 82, 0.14);
}

.start-button {
  min-width: 180px;
  border: 0;
  border-radius: 8px;
  padding: 9px 18px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  font-size: 0.98rem;
  font-weight: 900;
}

.judge-flash {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0;
}

.judge-flash.correct,
.judge-flash.divisible {
  animation: flash 560ms ease-out;
}

.judge-flash.correct {
  background: var(--green);
}

.judge-flash.divisible {
  background: var(--yellow);
}

@keyframes flash {
  0% { opacity: 0; }
  18% { opacity: 0.86; }
  100% { opacity: 0; }
}

.equation {
  --factor-gutter: clamp(18px, 3.2vw, 28px);
  --factor-outline-pad: 6px;
  position: relative;
  z-index: 1;
  isolation: isolate;
  display: grid;
  gap: 14px;
  justify-content: center;
  justify-items: end;
  padding: var(--factor-gutter);
}

.factor-layer {
  position: absolute;
  inset: 0;
  z-index: 40;
  overflow: visible;
  pointer-events: none;
}

.factor-outline {
  position: absolute;
  border: 3px solid var(--factor);
  border-radius: 6px;
  background: transparent;
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.76);
}

.factor-outline.carry-in.carry-out {
  border-color: #5e9d82;
  background: transparent;
}

.factor-outline.factor-style-dynamic {
  --factor-notch: clamp(10px, 2.2vw, 18px);
  --factor-left-top: 0px;
  --factor-left-mid: 0px;
  --factor-right-top: 100%;
  --factor-right-mid: 100%;
  overflow: visible;
  border: 0;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
}

.factor-outline.factor-style-dynamic.carry-out {
  --factor-left-top: var(--factor-notch);
}

.factor-outline.factor-style-dynamic.carry-in {
  --factor-right-mid: calc(100% - var(--factor-notch));
}

.factor-outline.factor-style-dynamic::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 2;
  padding: 3px;
  background: var(--factor);
  clip-path: polygon(
    var(--factor-left-top) 0,
    var(--factor-right-top) 0,
    var(--factor-right-mid) 50%,
    var(--factor-right-top) 100%,
    var(--factor-left-top) 100%,
    var(--factor-left-mid) 50%
  );
  -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  -webkit-mask-composite: xor;
  mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  mask-composite: exclude;
}

.factor-outline.factor-style-dynamic::after {
  content: none;
}

.factor-outline.factor-style-dynamic .factor-divider {
  display: none;
}

.factor-divider {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 2px;
  background: rgba(74, 154, 104, 0.58);
}

.factor-outline.carry-out::before,
.factor-outline.carry-in::after {
  content: "";
  position: absolute;
  top: var(--factor-joint-y, 50%);
  width: 0;
  height: 0;
  transform: translateY(-50%);
}

.factor-outline.carry-out::before {
  left: -11px;
  z-index: 2;
  border-top: 8px solid transparent;
  border-bottom: 8px solid transparent;
  border-right: 11px solid rgba(24, 142, 174, 0.78);
  filter: drop-shadow(0 0 1px #fff) drop-shadow(0 0 2px rgba(24, 82, 102, 0.22));
}

.factor-outline.carry-in::after {
  right: -2px;
  z-index: 1;
  border-top: 11px solid transparent;
  border-bottom: 11px solid transparent;
  border-right: 15px solid rgba(238, 158, 44, 0.78);
  filter: drop-shadow(0 0 1px #fff) drop-shadow(0 0 2px rgba(120, 80, 10, 0.2));
  mix-blend-mode: multiply;
}

.factor-outline.factor-style-dynamic::before {
  inset: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: auto;
  height: auto;
  transform: none;
  border: 0;
  filter: none;
}

.factor-outline.factor-style-dynamic::after {
  content: none;
  border: 0;
  filter: none;
  mix-blend-mode: normal;
}

.insert-indicator {
  position: absolute;
  z-index: 6;
  width: 4px;
  border-radius: 999px;
  background: #3167d6;
  box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.92), 0 0 0 6px rgba(49, 103, 214, 0.16);
  pointer-events: none;
}

.insert-indicator::before,
.insert-indicator::after {
  content: "";
  position: absolute;
  left: 50%;
  width: 0;
  height: 0;
  transform: translateX(-50%);
  border-left: 7px solid transparent;
  border-right: 7px solid transparent;
}

.insert-indicator::before {
  top: -8px;
  border-bottom: 8px solid #3167d6;
}

.insert-indicator::after {
  bottom: -8px;
  border-top: 8px solid #3167d6;
}

.row,
.bottom-slots {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: var(--slot-size);
  gap: var(--slot-gap);
  align-items: center;
}

.equation {
  --slot-size: clamp(44px, 9vw, 74px);
  --slot-gap: clamp(6px, 1.6vw, 12px);
}

.top-row {
  grid-template-columns: repeat(var(--top-digits, 6), var(--slot-size));
  width: max-content;
}

.bottom-row {
  grid-template-columns: auto auto auto auto;
  justify-content: start;
  justify-self: end;
  column-gap: var(--slot-gap);
}

.bottom-row .times {
  margin-right: calc(var(--slot-size) * 0.7 + var(--slot-gap));
}

.bottom-slots {
  grid-template-columns: repeat(var(--bottom-digits, 5), var(--slot-size));
  width: max-content;
}

.equals,
.times {
  font-size: clamp(1rem, 2.7vw, 1.55rem);
  font-weight: 800;
}

.fixed-multiplier {
  display: grid;
  place-items: center;
  width: calc(var(--slot-size) * 0.76);
  height: calc(var(--slot-size) * 0.76);
  border-radius: 8px;
  background: #e9eefc;
  border: 2px solid #b9c8f4;
  color: #244aa5;
  font-size: clamp(1rem, 3vw, 1.55rem);
  font-weight: 900;
}

.slot {
  position: relative;
  display: grid;
  place-items: center;
  width: var(--slot-size);
  height: var(--slot-size);
  border: 2px dashed #c5ccdb;
  border-radius: 8px;
  background: var(--slot);
}

.slot.drag-over {
  border-color: var(--accent);
  background: #eef4ff;
}

.slot.covered {
  opacity: 0.72;
}

.slot::after {
  content: attr(data-col);
  position: absolute;
  right: 5px;
  bottom: 3px;
  color: #bac1cf;
  font-size: 0.62rem;
  font-weight: 700;
}

.card-zone {
  margin-top: 16px;
}

.zone-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
  color: var(--muted);
  font-weight: 700;
}

.zone-head button {
  cursor: pointer;
}

.pool {
  --pool-card-size: min(70px, 18vw);
  --pool-card-gap: 0px;
  min-height: 104px;
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: var(--pool-card-gap);
  padding: 14px;
  border: 2px dashed #c9d0dd;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.7);
}

.pool.drag-over {
  border-color: var(--accent);
  background: #eef4ff;
}

.card {
  --span: 1;
  position: relative;
  display: grid;
  grid-template-rows: 1fr auto 1fr;
  place-items: center;
  width: calc(var(--pool-card-size, min(70px, 18vw)) * var(--span) + var(--pool-card-gap, 0px) * (var(--span) - 1));
  height: var(--pool-card-size, min(70px, 18vw));
  min-width: 44px;
  min-height: 44px;
  border: 2px solid #d7c391;
  border-radius: 8px;
  background: var(--card);
  box-shadow: 0 5px 13px rgba(62, 50, 27, 0.12);
  color: #2d2b25;
  cursor: grab;
  font-size: clamp(1.35rem, 4vw, 2.15rem);
  font-weight: 900;
  user-select: none;
  touch-action: none;
}

.card-main {
  grid-row: 2;
  line-height: 1;
}

.card-hints {
  width: 100%;
  min-height: 0.95em;
  color: #60706b;
  font-size: clamp(0.56rem, 1.8vw, 0.72rem);
  font-weight: 800;
  line-height: 1;
  overflow: hidden;
  text-align: center;
  white-space: nowrap;
}

.card-hints[hidden] {
  visibility: hidden;
}

.card-hints-top {
  grid-row: 1;
  align-self: end;
}

.card-hints-bottom {
  grid-row: 3;
  align-self: start;
}

.slot .card {
  width: calc(var(--slot-size) * var(--span) + var(--slot-gap) * (var(--span) - 1) - 8px);
  height: calc(var(--slot-size) - 8px);
  min-width: 0;
  min-height: 0;
  box-shadow: none;
  z-index: 1;
}

.card:active {
  cursor: grabbing;
}

.card.dragging-card {
  position: fixed;
  z-index: 20;
  pointer-events: none;
  opacity: 1;
  box-shadow: 0 12px 26px rgba(36, 48, 82, 0.2);
}

.dragging-factor {
  position: fixed;
  z-index: 30;
  pointer-events: none;
}

.dragging-factor-outline {
  position: absolute;
  z-index: 5;
}

.dragging-factor-card {
  position: absolute;
  z-index: 2;
  pointer-events: none;
}

.card.factor {
  background: var(--card);
}

.card.in-group {
  box-shadow: none;
}

.result-strip {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 14px;
  min-height: 44px;
}

#feedback {
  color: var(--muted);
  font-weight: 700;
}

.history {
  margin-top: 20px;
  color: var(--muted);
}

.history h2 {
  margin-bottom: 8px;
  font-size: 1rem;
}

#history-list {
  display: grid;
  gap: 6px;
  font-size: 0.9rem;
}

.history-entry {
  padding: 8px 10px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.72);
}

@media (max-width: 700px) {
  .topbar,
  .result-strip {
    align-items: stretch;
    flex-direction: column;
  }

  .stats {
    justify-content: flex-start;
  }

  .playfield {
    overflow-x: auto;
    padding-inline: 10px;
  }

  .equation {
    width: max-content;
    min-width: 100%;
  }
}

@media (max-width: 360px) {
  .app-shell {
    width: min(100% - 8px, 1040px);
  }

  .start-panel {
    padding: 3px;
  }

  .playfield.menu-open {
    min-height: 220px;
  }

  .setting-toggle {
    grid-template-columns: 4.4em minmax(0, 1fr);
    gap: 3px;
    padding: 4px;
  }

  .switch-control span {
    min-height: 22px;
    padding: 0 2px;
  }

  .start-button {
    min-width: 150px;
    padding: 8px 14px;
  }
}
