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

html, body {
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: rgb(221, 226, 219);
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

#rotate-prompt {
  display: none;
}

#app {
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--vh, 100dvh);
  max-width: 960px;
  margin: 0 auto;
  padding: 6px 4px;
  padding-top: max(6px, env(safe-area-inset-top));
  padding-right: max(4px, env(safe-area-inset-right));
  padding-bottom: max(6px, env(safe-area-inset-bottom));
  padding-left: max(4px, env(safe-area-inset-left));
  gap: 4px;
  box-sizing: border-box;
}

/* --- Header row: same width as rack --- */

#header-row,
#main-row {
  display: flex;
  flex-direction: row;
  align-items: stretch;
  gap: 0;
  flex: 0 0 auto;
  min-width: 0;
  width: 100%;
}

.side-gutter {
  flex: 0 0 var(--gutter-w, calc(100% / 15));
  width: var(--gutter-w, calc(100% / 15));
  min-width: 0;
}

#target-hand {
  flex: 0 0 var(--tiles-span-w, calc(100% * 14 / 15));
  width: var(--tiles-span-w, calc(100% * 14 / 15));
  max-width: var(--tiles-span-w, calc(100% * 14 / 15));
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  background: rgba(255, 255, 255, 0.55);
  border-radius: 6px;
  padding: 3px 8px;
  min-width: 0;
  box-sizing: border-box;
}

#hand-main {
  display: flex;
  align-items: baseline;
  gap: 10px;
  min-width: 0;
  flex: 1 1 auto;
}

#hand-text {
  font-family: 'Fredoka', sans-serif;
  font-size: clamp(16px, 2.8vw, 24px);
  font-weight: 500;
  letter-spacing: 0.04em;
  color: #222;
  line-height: 1.15;
  min-width: 0;
  flex: 0 1 auto;
  white-space: nowrap;
}

.hand-star {
  color: #d4a017;
  font-size: 0.9em;
  margin-left: 0.25em;
}

#hand-note {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 12px;
  color: #666;
  flex: 0 1 auto;
}

#hand-meta {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin-top: 0;
  font-size: 12px;
  color: #666;
  min-width: 0;
  flex: 0 0 auto;
  justify-content: flex-end;
  margin-left: 10px;
}

#hand-section,
#hand-concealed {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: right;
}

#hand-concealed:not(:empty)::before {
  content: ' · ';
}

.mask-g { color: green; }
.mask-r { color: red; }

/* --- Message (between rack and palette) --- */

#message {
  flex-shrink: 0;
  min-height: 0;
  display: none;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-size: 13px;
  font-weight: 600;
  color: #555;
  line-height: 1.2;
  background: transparent;
}

#message:not(:empty) {
  display: flex;
  min-height: 16px;
  margin: 2px 0;
}

#message.error {
  color: #b33;
}

#message.success {
  color: #2a7a3a;
}

#palette-block {
  display: flex;
  flex-direction: row;
  align-items: flex-end;
  gap: 8px;
  width: 100%;
  min-width: 0;
}

#rack-actions {
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: calc(var(--tile-w, 28px) * 2 + 2px);
  flex: 0 0 auto;
}

/* --- Center column: no shared panel — only rack/palette own boxes --- */

#center-col {
  display: flex;
  flex-direction: column;
  gap: 0;
  flex: 0 0 var(--tiles-span-w, calc(100% * 14 / 15));
  width: var(--tiles-span-w, calc(100% * 14 / 15));
  max-width: var(--tiles-span-w, calc(100% * 14 / 15));
  min-width: 0;
  background: transparent;
  padding: 0;
  margin: 0;
  border: none;
  box-shadow: none;
}

#rack {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: flex-start;
  gap: 2px;
  flex: 0 0 auto;
  width: 100%;
  min-width: 0;
  background: rgba(255, 255, 255, 0.55);
  border-radius: 6px;
  padding: 3px 0;
  overflow: hidden;
  box-sizing: border-box;
}

.rack-slot {
  aspect-ratio: 3 / 4;
  width: var(--tile-w, calc((100% - 26px) / 14));
  height: var(--tile-h, calc(var(--tile-w, 36px) * 4 / 3));
  max-height: var(--tile-h, calc(var(--tile-w, 36px) * 4 / 3));
  flex: 0 0 var(--tile-w, calc((100% - 26px) / 14));
  align-self: flex-start;
  min-width: 0;
  border: none;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  cursor: pointer;
  box-sizing: border-box;
  padding: 1px;
}

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

#palette {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  flex: 0 0 auto;
  width: max-content;
  max-width: 100%;
  min-width: 0;
  background: rgba(255, 255, 255, 0.55);
  border-radius: 6px;
  padding: 3px 0;
  overflow: hidden;
  box-sizing: border-box;
}

.tile-row {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: flex-start;
  gap: 2px;
  flex: 0 0 auto;
  width: max-content;
  max-width: 100%;
  min-width: 0;
  overflow: hidden;
}

.tile-btn {
  border: none;
  background: #fff;
  border-radius: 4px;
  padding: 1px;
  cursor: pointer;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12);
  aspect-ratio: 3 / 4;
  width: var(--palette-tile-w, var(--tile-w, calc((100% - 26px) / 14)));
  height: var(--palette-tile-h, var(--tile-h, calc(var(--tile-w, 36px) * 4 / 3)));
  max-height: var(--palette-tile-h, var(--tile-h, calc(var(--tile-w, 36px) * 4 / 3)));
  flex: 0 0 var(--palette-tile-w, var(--tile-w, calc((100% - 26px) / 14)));
  align-self: flex-start;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.08s ease, box-shadow 0.08s ease;
}

.tile-btn.empty-slot {
  visibility: hidden;
  pointer-events: none;
  box-shadow: none;
  background: transparent;
}

.tile-btn.exhausted {
  visibility: hidden;
  pointer-events: none;
}

.tile-btn:active {
  transform: scale(0.93);
  box-shadow: 0 0 1px rgba(0, 0, 0, 0.2);
}

.tile-btn:focus {
  outline: none;
}

.tile-btn:disabled {
  opacity: 0.45;
  cursor: default;
}

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

/* --- Rack actions (stacked beside rack) + logo --- */

.bar-btn {
  width: 100%;
  padding: 8px 2px;
  border: 1px solid #ccc;
  border-radius: 5px;
  background: #fff;
  font-size: 13px;
  line-height: 1.2;
  color: #333;
  cursor: pointer;
  white-space: nowrap;
  text-align: center;
  box-sizing: border-box;
}

.bar-btn:active {
  background: #f0f0f0;
}

.bar-btn:focus {
  outline: none;
}

#app-menu {
  position: fixed;
  inset: 0;
  z-index: 350;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  padding-top: max(16px, env(safe-area-inset-top));
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  box-sizing: border-box;
}

#app-menu.hidden {
  display: none;
}

.menu-panel {
  position: relative;
  z-index: 1;
  width: min(320px, 100%);
  max-height: min(80dvh, 560px);
  display: flex;
  flex-direction: column;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.18);
  overflow: hidden;
  box-sizing: border-box;
}

.menu-panel.is-tracker,
.menu-panel.is-help {
  width: min(720px, 100%);
  max-height: min(86dvh, 640px);
}

.menu-panel.is-help {
  width: min(480px, 100%);
}

#menu-view-main.hidden,
#menu-view-section.hidden,
#menu-view-tracker.hidden,
#menu-view-help.hidden {
  display: none;
}

#menu-view-section {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

#menu-view-tracker,
#menu-view-help {
  padding: 18px 20px 22px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  min-height: 0;
}

#help-content {
  color: #333;
  font-size: 15px;
  line-height: 1.45;
}

#help-content h2 {
  font-family: 'Fredoka', sans-serif;
  font-size: 22px;
  font-weight: 500;
  color: #222;
  margin: 0 0 10px;
}

#help-content h3 {
  font-size: 15px;
  font-weight: 600;
  color: #222;
  margin: 18px 0 6px;
}

#help-content p {
  margin: 0 0 8px;
}

#help-content a {
  color: #1a5a8a;
  text-decoration: underline;
}

#tracker-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 22px 16px;
}

.tracker-cell {
  min-width: 0;
}

.tracker-label {
  font-size: 15px;
  font-weight: 500;
  color: #222;
  margin-bottom: 6px;
}

.tracker-stars {
  display: flex;
  flex-wrap: wrap;
  gap: 3px;
  max-width: 100%;
}

.tracker-star {
  width: 16px;
  height: 16px;
  color: #e4e4e4;
  font-size: 16px;
  line-height: 1;
}

.tracker-star.is-on {
  color: #d4a017;
}

#menu-section-list {
  flex: 1 1 auto;
  min-height: 0;
  max-height: min(70dvh, calc(var(--vh, 100dvh) * 0.72));
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.menu-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  margin: 0;
  padding: 14px 16px;
  border: none;
  background: transparent;
  color: #222;
  font-size: 16px;
  text-align: left;
  cursor: pointer;
}

.menu-row + .menu-row {
  border-top: 1px solid #eee;
}

.menu-row:active {
  background: #f5f5f5;
}

.menu-row-value {
  color: #888;
  font-weight: 400;
}

.menu-row.is-selected {
  font-weight: 600;
}

#palette .tile-row:nth-child(n+4) {
  display: none;
}

/* --- Portrait: 7-wide edge-to-edge, buttons locked at bottom --- */

body.is-portrait #app {
  max-width: none;
  visibility: visible;
  padding-left: max(18px, env(safe-area-inset-left));
  padding-right: max(18px, env(safe-area-inset-right));
}

body.is-portrait #header-row,
body.is-portrait #main-row {
  width: 100%;
}

body.is-portrait .side-gutter {
  display: none;
}

body.is-portrait #target-hand {
  flex: 1 1 auto;
  width: 100%;
  max-width: none;
}

body.is-portrait #main-row {
  flex: 1 1 auto;
  min-height: 0;
  flex-direction: column;
  align-items: stretch;
}

body.is-portrait #center-col {
  flex: 1 1 auto;
  width: 100%;
  max-width: none;
  min-height: 0;
  justify-content: flex-start;
}

body.is-portrait #rack {
  display: grid;
  grid-template-columns: repeat(7, var(--tile-w));
  justify-content: start;
  justify-items: start;
  column-gap: 2px;
  row-gap: 2px;
  width: 100%;
}

body.is-portrait #palette .tile-row:nth-child(n+4) {
  display: flex;
}

body.is-portrait #palette-block {
  flex: 1 1 auto;
  flex-direction: column;
  align-items: stretch;
  gap: 0;
  min-height: 0;
}

body.is-portrait #rack-actions {
  flex-direction: row;
  width: 100%;
  gap: 8px;
  margin-top: auto;
  padding-top: 8px;
}

body.is-portrait .bar-btn {
  flex: 1 1 0;
  padding: 12px 8px;
  font-size: 15px;
}

body.is-portrait #hand-text {
  font-size: clamp(15px, 4.5vw, 20px);
  white-space: nowrap;
}

body.is-portrait .menu-panel.is-tracker,
body.is-portrait .menu-panel.is-help {
  width: 100%;
}

body.is-portrait #tracker-grid {
  gap: 18px 10px;
}

body.is-portrait .tracker-label {
  font-size: 13px;
}

body.is-portrait .tracker-star {
  width: 14px;
  height: 14px;
  font-size: 14px;
}

body.is-portrait #hand-meta {
  flex-direction: column;
  align-items: flex-end;
  gap: 0;
  line-height: 1.2;
}

body.is-portrait #hand-concealed:not(:empty)::before {
  content: none;
}

body.is-portrait #hand-main {
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
}

body.is-portrait #hand-note,
body.is-portrait #hand-meta {
  font-size: 11px;
}

body.is-portrait #hand-note {
  white-space: normal;
}

body.is-portrait #error-rack,
body.is-portrait #error-closest {
  flex-wrap: wrap;
  justify-content: flex-start;
  width: calc(100% - 32px);
  max-width: 100%;
  box-sizing: border-box;
}

body.is-portrait #error-rack img,
body.is-portrait #error-closest img {
  width: calc((100% - 18px) / 7);
  max-width: none;
}

@media (max-height: 500px) {
  #app {
    padding: 4px;
    gap: 2px;
  }

  #target-hand {
    padding: 2px 8px;
  }

  #hand-text {
    font-size: clamp(14px, 2.6vw, 20px);
  }

  #hand-meta {
    font-size: 10px;
    gap: 8px;
  }

  #message:not(:empty) {
    min-height: 14px;
    font-size: 11px;
  }

  #center-col {
    gap: 0;
  }

  #rack,
  #palette {
    padding: 2px 0;
    gap: 2px;
  }

  .tile-row {
    gap: 2px;
  }

  #rack-actions {
    gap: 5px;
  }

  .bar-btn {
    padding: 6px 1px;
    font-size: 12px;
  }
}

/* Dark / solid tile styles */
body.style-dark,
body.style-solid {
  background: #1a1a1a;
}

body.style-dark #target-hand,
body.style-dark #rack,
body.style-dark #palette,
body.style-solid #target-hand,
body.style-solid #rack,
body.style-solid #palette {
  background: rgba(255, 255, 255, 0.08);
}

body.style-dark .rack-slot,
body.style-solid .rack-slot {
  background: rgba(255, 255, 255, 0.12);
}

body.style-dark #hand-text,
body.style-solid #hand-text {
  color: #eee;
}

body.style-dark #hand-meta,
body.style-solid #hand-meta {
  color: #aaa;
}

body.style-dark #message,
body.style-solid #message {
  color: #ccc;
}

/* --- First-run welcome --- */

#welcome-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 450;
  align-items: safe center;
  justify-content: center;
  padding: 16px;
  padding-top: max(16px, env(safe-area-inset-top));
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  box-sizing: border-box;
}

#welcome-overlay.visible {
  display: flex;
}

#welcome-box {
  max-width: 480px;
  width: 100%;
  margin: auto 0;
  background: #fff;
  border-radius: 12px;
  padding: 20px 22px 18px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
  color: #333;
}

#welcome-box h2 {
  font-family: 'Fredoka', sans-serif;
  font-weight: 500;
  margin: 0 0 8px;
  font-size: 22px;
  color: #222;
}

#welcome-box h3 {
  margin: 14px 0 6px;
  font-size: 15px;
}

#welcome-box p {
  margin: 0 0 8px;
  font-size: 14px;
  line-height: 1.5;
}

#welcome-box ul {
  margin: 0 0 10px;
  padding-left: 20px;
  font-size: 14px;
  line-height: 1.5;
}

#welcome-box li {
  margin-bottom: 6px;
}

.welcome-tip {
  font-size: 13px;
  color: #555;
  font-style: italic;
}

.welcome-ok-btn {
  display: block;
  width: 100%;
  margin-top: 12px;
  padding: 10px;
  border: none;
  border-radius: 8px;
  background: rgb(90, 130, 100);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

.welcome-ok-btn:active {
  transform: scale(0.98);
}

/* --- Result modals (correct / error) --- */

#success-modal,
#error-modal {
  position: fixed;
  inset: 0;
  z-index: 400;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  padding-top: max(16px, env(safe-area-inset-top));
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  box-sizing: border-box;
}

#success-modal.hidden,
#error-modal.hidden {
  display: none;
}

.result-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(40, 50, 42, 0.45);
}

.result-panel {
  position: relative;
  z-index: 1;
  width: min(720px, 100%);
  max-height: min(86dvh, 640px);
  display: flex;
  flex-direction: column;
  background: #f4f6f3;
  border-radius: 14px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.22);
  overflow: hidden;
  box-sizing: border-box;
}

#error-panel {
  max-height: min(86dvh, 640px);
  overflow: hidden;
}

#error-panel .result-scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
}

#error-panel .result-sets {
  flex: 0 0 auto;
  overflow: visible;
  min-height: 0;
}

.result-close {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 2;
  width: 32px;
  height: 32px;
  border: 1.5px solid #bbb;
  border-radius: 50%;
  background: #fff;
  color: #444;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.result-close:active {
  background: #eee;
}

.result-header {
  flex: 0 0 auto;
  padding: 18px 48px 12px 20px;
  text-align: center;
  background: rgba(255, 255, 255, 0.7);
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
}

.result-title {
  font-family: 'Fredoka', sans-serif;
  font-size: clamp(26px, 4vw, 34px);
  font-weight: 500;
  margin: 0 0 8px;
  letter-spacing: 0.02em;
}

.result-title.success {
  color: #2a7a3a;
}

.result-title.error {
  color: #b33;
}

.result-hand-text {
  font-family: 'Fredoka', sans-serif;
  font-size: clamp(18px, 3vw, 24px);
  font-weight: 500;
  letter-spacing: 0.04em;
  color: #222;
  line-height: 1.2;
}

.result-hand-meta {
  margin-top: 6px;
  font-size: 13px;
  color: #666;
}

.result-sets-label {
  flex: 0 0 auto;
  padding: 10px 20px 4px;
  font-size: 12px;
  font-weight: 600;
  color: #666;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.result-sets {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 4px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.result-footnote {
  flex: 0 0 auto;
  margin: 0;
  padding: 8px 20px 14px;
  font-size: 12px;
  color: #666;
  text-align: center;
  font-style: italic;
  background: #f4f6f3;
  border-top: 1px solid rgba(0, 0, 0, 0.06);
}

.result-footnote.hidden {
  display: none;
}

.result-tile-row {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 3px;
  margin: 0 16px 10px;
  padding: 6px 8px;
  background: rgba(255, 255, 255, 0.85);
  border-radius: 6px;
  flex: 0 0 auto;
}

.result-tile-row img {
  width: clamp(22px, 4vw, 34px);
  height: auto;
  aspect-ratio: 3 / 4;
  object-fit: contain;
  display: block;
  flex: 0 0 auto;
  border-radius: 2px;
  background: #fff;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.08);
}

.result-tile-row img.wrong {
  opacity: 0.28;
  filter: grayscale(0.7);
}

.result-tile-row img.right {
  outline: 2px solid rgba(42, 122, 58, 0.55);
  outline-offset: -1px;
}

.result-tile-row img.missing {
  outline: 2px solid #111;
  outline-offset: -1px;
}

.success-set-row {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 3px;
  padding: 4px 6px;
  background: rgba(255, 255, 255, 0.85);
  border-radius: 6px;
  flex-shrink: 0;
}

.success-set-row.match {
  outline: 2px solid rgba(42, 122, 58, 0.45);
  outline-offset: -1px;
}

.success-set-row img {
  width: clamp(18px, 3.2vw, 28px);
  height: auto;
  aspect-ratio: 3 / 4;
  object-fit: contain;
  display: block;
  flex: 0 0 auto;
  border-radius: 2px;
  background: #fff;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.08);
}

body.style-dark .result-panel,
body.style-solid .result-panel {
  background: #222;
}

body.style-dark .result-header,
body.style-solid .result-header {
  background: rgba(255, 255, 255, 0.06);
  border-bottom-color: rgba(255, 255, 255, 0.08);
}

body.style-dark .result-hand-text,
body.style-solid .result-hand-text {
  color: #eee;
}

body.style-dark .result-footnote,
body.style-solid .result-footnote {
  background: #222;
  border-top-color: rgba(255, 255, 255, 0.08);
}

body.style-dark .result-hand-meta,
body.style-dark .result-sets-label,
body.style-dark .result-footnote,
body.style-solid .result-hand-meta,
body.style-solid .result-sets-label,
body.style-solid .result-footnote {
  color: #aaa;
}

body.style-dark .success-set-row,
body.style-dark .result-tile-row,
body.style-solid .success-set-row,
body.style-solid .result-tile-row {
  background: rgba(255, 255, 255, 0.08);
}

body.style-dark .result-close,
body.style-solid .result-close {
  background: #333;
  border-color: #555;
  color: #ddd;
}
