:root {
  color-scheme: light;
  --bg: #f7f6f2;
  --surface: #ffffff;
  --surface-muted: #f1f0ea;
  --ink: #171717;
  --muted: #5f625c;
  --faint: #8a8d86;
  --line: #dedbd2;
  --line-strong: #c9c4b7;
  --accent: #b88917;
  --accent-strong: #8f6507;
  --accent-soft: #fbf2d9;
  --success: #0f766e;
  --success-soft: #e7f3f1;
  --danger: #b42318;
  --danger-soft: #fff0ed;
  --picker-height: 258px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html {
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  overflow-x: hidden;
}

h1,
h2,
h3,
p {
  margin: 0;
}

button,
select,
input {
  font: inherit;
}

button,
select {
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
}

button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 0 12px;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  transition: background-color 180ms ease, border-color 180ms ease, color 180ms ease, opacity 180ms ease;
}

button:hover {
  border-color: var(--line-strong);
  background: var(--surface-muted);
}

button:focus-visible,
input:focus-visible,
summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

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

svg {
  width: 18px;
  height: 18px;
  flex: 0 0 auto;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.skip-link {
  position: fixed;
  left: 12px;
  top: 12px;
  z-index: 20;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--ink);
  color: #fff;
  transform: translateY(-150%);
  transition: transform 180ms ease;
}

.skip-link:focus {
  transform: translateY(0);
}

.app {
  width: min(1180px, 100%);
  margin: 0 auto;
  padding: 18px 14px calc(var(--picker-height) + 28px + env(safe-area-inset-bottom));
}

.topbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 14px;
  margin-bottom: 14px;
}

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

.brand-mark {
  display: grid;
  width: 42px;
  height: 42px;
  flex: 0 0 auto;
  place-items: center;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: var(--ink);
  color: #fff;
  font-weight: 800;
}

.eyebrow,
.section-kicker {
  color: var(--accent-strong);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0;
  line-height: 1.2;
  text-transform: uppercase;
}

h1 {
  margin-top: 2px;
  font-size: 23px;
  line-height: 1.18;
}

h2 {
  font-size: 17px;
  line-height: 1.25;
}

h3 {
  margin-bottom: 8px;
  font-size: 14px;
  line-height: 1.3;
}

.status-pill {
  justify-self: center;
  max-width: 100%;
  min-height: 38px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  line-height: 1.35;
}

.icon-button span {
  white-space: nowrap;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(320px, 380px);
  gap: 14px;
  align-items: start;
}

.main-column,
.side-column {
  display: grid;
  gap: 12px;
}

.side-column {
  position: sticky;
  top: 14px;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
}

.hand-panel {
  min-height: 132px;
}

.meld-panel {
  min-height: 136px;
}

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

.hint {
  max-width: 360px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  line-height: 1.45;
}

.ghost {
  background: transparent;
}

.primary {
  width: 100%;
  border-color: var(--ink);
  background: var(--ink);
  color: #fff;
  font-size: 18px;
}

.primary:hover,
.primary:focus-visible {
  border-color: var(--accent-strong);
  background: var(--accent-strong);
  color: #fff;
}

.selected-tiles,
.melds,
.mini-tiles {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  min-height: 54px;
  align-items: center;
}

.empty {
  color: var(--muted);
}

.selected-tiles.empty,
.melds.empty,
.mini-tiles.empty {
  border: 1px dashed var(--line-strong);
  border-radius: 8px;
  background: var(--surface-muted);
  padding: 12px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 650;
}

.mini-tiles.empty {
  min-height: 44px;
  padding: 10px;
}

.tile {
  position: relative;
  width: 38px;
  height: 51px;
  min-height: 0;
  padding: 0;
  border: 0;
  border-radius: 5px;
  background: transparent;
  flex: 0 0 auto;
}

.tile:hover,
.tile:focus-visible {
  background: var(--accent-soft);
}

.tile img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
}

.tile.win::after {
  content: "";
  position: absolute;
  inset: -3px;
  border: 3px solid var(--danger);
  border-radius: 8px;
  pointer-events: none;
}

.tile .badge {
  position: absolute;
  right: -4px;
  top: -5px;
  min-width: 18px;
  height: 18px;
  padding: 0 4px;
  border-radius: 9px;
  background: var(--danger);
  color: #fff;
  font-size: 11px;
  line-height: 18px;
}

.meld-card {
  display: flex;
  align-items: center;
  gap: 8px;
  max-width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
  background: #fff;
}

.meld-card.pending {
  border-style: dashed;
  background: var(--success-soft);
}

.meld-label {
  min-width: 44px;
  color: var(--success);
  font-size: 13px;
  font-weight: 800;
}

.meld-upgrade {
  border-color: rgba(15, 118, 110, 0.36);
  color: var(--success);
}

.calc-memory {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  min-height: 34px;
  margin-top: 12px;
  align-items: center;
  color: var(--muted);
  font-size: 13px;
}

.calc-memory.empty {
  min-height: 0;
}

.memory-item {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 42px;
  padding: 5px 6px 5px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
}

.memory-label {
  color: var(--muted);
  font-weight: 800;
}

.memory-item .tile {
  width: 30px;
  height: 40px;
}

.memory-item button:not(.tile) {
  min-height: 32px;
  padding: 0 9px;
}

details summary {
  display: flex;
  min-height: 42px;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  cursor: pointer;
  font-size: 17px;
  font-weight: 800;
  list-style: none;
}

details summary::-webkit-details-marker {
  display: none;
}

details summary::after {
  content: "";
  width: 8px;
  height: 8px;
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  transform: rotate(45deg);
  transition: transform 180ms ease;
}

details[open] summary::after {
  transform: rotate(225deg);
}

details summary small {
  margin-left: auto;
  color: var(--accent-strong);
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
}

.settings {
  display: grid;
  gap: 12px;
  padding-top: 12px;
}

.field,
.switch,
.stepper {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.field > span,
.switch > span,
.stepper > span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 5px;
  min-width: 160px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-muted);
}

.segmented button {
  min-height: 36px;
  border-color: transparent;
  background: transparent;
  padding: 0 9px;
}

.mode-btn.active,
.meld-tools button.active,
.segmented button.active,
.target-tabs button.active,
.wind-tabs button.active {
  border-color: var(--accent);
  background: var(--surface);
  color: var(--accent-strong);
}

.target-tabs {
  grid-template-columns: repeat(4, minmax(0, 1fr));
  min-width: min(420px, 100%);
}

.wind-tabs {
  grid-template-columns: repeat(4, minmax(0, 1fr));
  min-width: 220px;
}

.step-row {
  display: grid;
  grid-template-columns: 1fr;
}

.stepper {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px;
  background: var(--surface-muted);
}

.stepper button {
  width: 40px;
  min-height: 36px;
  padding: 0;
  background: var(--surface);
}

.stepper strong {
  min-width: 26px;
  text-align: center;
}

.flag-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.flag {
  min-height: 40px;
  padding: 0 8px;
  font-size: 13px;
}

.flag.active {
  border-color: var(--success);
  background: var(--success-soft);
  color: var(--success);
}

.flag:disabled {
  opacity: 0.45;
}

.dora-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.meld-dora-row {
  margin-top: 12px;
}

.dora-box {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  background: var(--surface);
}

.mini-tiles .tile {
  width: 30px;
  height: 40px;
}

.option-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.switch {
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 10px;
  background: var(--surface);
}

.switch input {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
  cursor: pointer;
}

.result-panel {
  min-height: 116px;
}

.empty-state {
  display: grid;
  gap: 6px;
}

.empty-state strong {
  color: var(--ink);
  font-size: 18px;
}

.empty-state span {
  color: var(--muted);
  font-size: 14px;
}

.result-panel strong {
  display: block;
  font-size: 25px;
  line-height: 1.25;
}

.result-panel p {
  margin-top: 6px;
  color: var(--muted);
  font-weight: 700;
}

.result-panel h3 {
  margin-top: 14px;
}

.result-panel ul {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  margin: 8px 0 0;
  padding: 0;
  list-style: none;
}

.result-panel li {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 9px;
  background: var(--surface-muted);
  color: var(--ink);
  font-size: 13px;
  font-weight: 700;
}

.result-error {
  background: var(--danger-soft);
  border-color: rgba(180, 35, 24, 0.28);
}

.choice-dialog {
  width: min(420px, calc(100% - 32px));
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0;
  color: var(--ink);
}

.choice-dialog::backdrop {
  background: rgba(23, 23, 23, 0.42);
}

.choice-dialog form {
  display: grid;
  gap: 12px;
  padding: 16px;
}

.choice-dialog h2 {
  font-size: 18px;
}

.choice-body {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.choice-body.count-choice-body {
  display: grid;
  gap: 12px;
}

.count-choice-note {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.choice-body > button:not(.tile) {
  flex: 1 1 110px;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
}

.count-choice-row {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 8px;
}

.count-choice {
  min-height: 46px;
  padding: 0;
  border-radius: 8px;
  background: var(--surface-muted);
  font-size: 17px;
}

.count-choice:hover,
.count-choice:focus-visible {
  border-color: var(--accent);
  color: var(--accent-strong);
}

.count-choice.recommended {
  border-color: rgba(184, 137, 23, 0.35);
  background: var(--surface);
}

.choice-dialog.count-dialog .dialog-actions {
  padding-top: 2px;
  border-top: 1px solid var(--line);
}

.picker {
  position: fixed;
  left: 50%;
  bottom: 12px;
  z-index: 10;
  width: min(1120px, calc(100% - 24px));
  transform: translateX(-50%);
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.96);
  padding: 10px 12px calc(12px + env(safe-area-inset-bottom));
  backdrop-filter: blur(12px);
}

.picker-targets {
  grid-template-columns: repeat(4, 1fr);
  min-width: 0;
  margin-bottom: 8px;
}

#tilePicker {
  display: grid;
  gap: 6px;
  margin-bottom: 8px;
}

.picker-row {
  display: grid;
  grid-template-columns: 26px minmax(0, 1fr);
  align-items: center;
  gap: 7px;
}

.picker-row span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
  text-align: center;
}

.picker-tiles {
  display: grid;
  grid-template-columns: repeat(10, minmax(0, 1fr));
  gap: 4px;
  min-width: 0;
  padding-bottom: 2px;
}

.picker .tile {
  width: 100%;
  height: auto;
  aspect-ratio: 34 / 46;
}

.picker-row:last-child .picker-tiles {
  grid-template-columns: repeat(7, minmax(0, 1fr));
}

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

.picker.collapsed .picker-targets,
.picker.collapsed #tilePicker {
  display: none;
}

@media (min-width: 560px) {
  .tile {
    width: 43px;
    height: 58px;
  }

  .picker .tile {
    width: 100%;
    height: auto;
  }
}

@media (max-width: 980px) {
  .status-pill {
    grid-column: 1 / -1;
    justify-self: stretch;
  }

  .workspace {
    grid-template-columns: 1fr;
  }

  .side-column {
    position: static;
  }
}

@media (max-width: 560px) {
  .app {
    padding: 12px 10px calc(var(--picker-height) + 22px + env(safe-area-inset-bottom));
  }

  .topbar {
    gap: 10px;
  }

  .brand-mark {
    width: 38px;
    height: 38px;
  }

  h1 {
    font-size: 20px;
  }

  .icon-button {
    width: 42px;
    padding: 0;
  }

  .icon-button span {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
  }

  .panel {
    padding: 12px;
  }

  .section-title {
    flex-direction: column;
    gap: 6px;
  }

  .hint {
    max-width: none;
  }

  .field {
    align-items: stretch;
    flex-direction: column;
  }

  .field > .segmented,
  .wind-tabs {
    width: 100%;
    min-width: 0;
  }

  .flag-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .dora-row,
  .option-row {
    grid-template-columns: 1fr;
  }

  .count-choice-row {
    grid-template-columns: repeat(5, minmax(0, 1fr));
  }

  .picker-actions {
    grid-template-columns: 76px 42px 42px minmax(0, 1fr);
  }

  .picker {
    bottom: 0;
    width: 100%;
    border-right: 0;
    border-bottom: 0;
    border-left: 0;
    border-radius: 8px 8px 0 0;
    padding-right: 10px;
    padding-left: 10px;
  }
}

@media (max-width: 380px) {
  .target-tabs button,
  .flag,
  .segmented button {
    padding-right: 6px;
    padding-left: 6px;
    font-size: 13px;
  }

  .picker-row {
    grid-template-columns: 22px minmax(0, 1fr);
    gap: 5px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}
