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

html, body {
  height: 100%;
  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;
}

#app {
  display: flex;
  flex-direction: column;
  height: var(--vh, 100dvh);
  max-width: 900px;
  margin: 0 auto;
  padding: 8px;
  gap: 3px;
}

/* --- Hand area --- */

#hand-area {
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
  background: rgba(255, 255, 255, 0.45);
  border-radius: 8px;
  padding: 6px 8px;
  overflow: hidden;
  min-width: 0;
}

/* --- Tile section: tile palette + score panel side by side --- */

#tile-section {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
  min-width: 0;
  overflow: hidden;
  max-width: 100%;
}

#hand-slots {
  display: grid;
  grid-template-columns: repeat(14, minmax(0, 1fr));
  gap: 3px;
  flex-shrink: 0;
}

/* --- Rack slots (reused for hand display) --- */

.rack-slot {
  aspect-ratio: 3 / 4;
  min-width: 0;
  border: none;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.rack-slot.filled {
  background: rgba(255, 255, 255, 0.7);
  padding: 1px;
}

.rack-slot img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

/* --- Score panel --- */

#score-panel {
  flex-shrink: 0;
  width: 72px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  background: rgba(255, 255, 255, 0.45);
  border-radius: 8px;
  padding: 8px 4px;
}

.score-label {
  font-size: 11px;
  font-weight: 600;
  color: #888;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.score-value {
  font-size: 28px;
  font-weight: 700;
  color: #333;
  line-height: 1.1;
}

.score-value.pts {
  color: #222;
}

/* --- Tile rows (shared button styles) --- */

.tile-btn {
  border: none;
  background: #fff;
  border-radius: 5px;
  padding: 2px;
  cursor: pointer;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
  aspect-ratio: 3 / 4;
  min-width: 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.08s ease, box-shadow 0.08s ease;
}

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

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

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

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

/* --- Landscape tile rows: 14-column grid, 3 rows of 12 --- */

#tile-rows-landscape {
  display: flex;
  flex-direction: column;
  gap: 3px;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.45);
  border-radius: 8px;
  padding: 6px 8px;
}

.tile-row {
  display: grid;
  grid-template-columns: repeat(14, minmax(0, 1fr));
  gap: 3px;
  overflow: hidden;
}

.tile-row::after {
  content: '';
  grid-column: 13 / 15;
}

/* --- Portrait tile rows: 7-column grid, 6 rows of 6 --- */

#tile-rows-portrait {
  display: none;
  flex-direction: column;
  gap: 3px;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.45);
  border-radius: 8px;
  padding: 6px 8px;
}

.tile-row-p {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 3px;
  overflow: hidden;
}

/* --- Button bar --- */

#button-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 0;
  flex-shrink: 0;
}

.bar-left, .bar-right {
  display: flex;
  gap: 6px;
}

.bar-center {
  flex: 1;
  text-align: center;
}

.app-title {
  font-family: 'Fredoka', sans-serif;
  font-size: 22px;
  font-weight: 500;
  color: #555;
}

.title-short {
  display: none;
}

.bar-btn {
  padding: 6px 14px;
  border: 1px solid #ccc;
  border-radius: 6px;
  background: #fff;
  font-size: 13px;
  color: #333;
  cursor: pointer;
  white-space: nowrap;
}

.bar-btn:disabled {
  opacity: 0.8;
  cursor: default;
}

/* --- Mobile landscape: fit everything in viewport column --- */

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

  #hand-area {
    padding: 3px 8px;
  }

  #tile-section {
    flex: 1 1 0%;
    min-height: 0;
  }

  #tile-rows-landscape {
    padding: 3px 6px;
    gap: 2px;
    flex: 1;
    min-height: 0;
  }

  .tile-row {
    display: flex;
    gap: 2px;
    flex: 1;
    min-height: 0;
  }

  .tile-row::after {
    display: none;
  }

  .tile-btn {
    aspect-ratio: 3 / 4;
    height: 100%;
    width: auto;
    min-width: 0;
  }

  #hand-slots {
    gap: 2px;
  }

  #score-panel {
    width: 56px;
    padding: 4px 2px;
  }

  .score-value {
    font-size: 20px;
  }

  .score-label {
    font-size: 9px;
  }

  #button-bar {
    padding: 2px 0;
  }

  .bar-btn {
    padding: 3px 10px;
    font-size: 11px;
  }

  .app-title {
    font-size: 16px;
  }
}

/* --- Mobile portrait: rack 2x7, tiles 6x6 --- */

@media (max-width: 600px) and (orientation: portrait) {
  #app {
    position: static;
  }

  #tile-section {
    flex: 1 1 0%;
    min-height: 0;
  }

  #tile-rows-portrait {
    display: flex;
    flex: 1;
    min-height: 0;
  }

  .tile-row-p {
    display: flex;
    gap: 3px;
    flex: 1;
    min-height: 0;
  }

  .tile-row-p .tile-btn {
    aspect-ratio: 3 / 4;
    height: 100%;
    width: auto;
    min-width: 0;
  }

  #hand-slots {
    grid-template-columns: repeat(7, minmax(0, 1fr));
  }

  #tile-rows-landscape {
    display: none;
    position: static;
  }

  #button-bar {
    position: static;
    width: auto;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    padding: 4px 0;
  }

  .bar-left, .bar-right {
    flex-direction: row;
    gap: 6px;
  }

  .bar-center {
    display: block;
  }

  .bar-btn {
    padding: 6px 14px;
    font-size: 13px;
    text-align: center;
  }

  #score-panel {
    width: 60px;
  }

  .title-full {
    display: none;
  }

  .title-short {
    display: inline;
  }
}

/* --- Help overlay --- */

#help-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 200;
  align-items: center;
  justify-content: center;
}

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

#help-box {
  background: rgb(221, 226, 219);
  border-radius: 12px;
  padding: 16px 20px;
  max-width: 480px;
  width: 90%;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  position: relative;
}

#help-close {
  position: absolute;
  top: 8px;
  right: 10px;
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  font-size: 20px;
  line-height: 32px;
  text-align: center;
  cursor: pointer;
}

#help-box h2 {
  font-size: 18px;
  margin: 0 0 10px;
  color: #222;
}

#help-box h3 {
  font-size: 14px;
  margin: 10px 0 4px;
  color: #333;
}

#help-box p {
  font-size: 13px;
  line-height: 1.5;
  color: #444;
  margin: 0 0 6px;
}

/* --- Win overlay --- */

#win-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 200;
  align-items: center;
  justify-content: center;
}

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

#win-box {
  background: rgb(221, 226, 219);
  border-radius: 12px;
  padding: 16px 20px;
  max-width: 480px;
  width: 90%;
  text-align: center;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

#win-title {
  font-size: 20px;
  font-weight: 700;
  color: #222;
  margin-bottom: 10px;
}

#win-hand-text {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 1px;
  margin-bottom: 8px;
}

#win-hand-text .mask-g { color: green; }
#win-hand-text .mask-r { color: red; }
#win-hand-text .mask-0 { color: #1a1a4e; }

#win-note {
  font-size: 14px;
  color: #444;
  margin-bottom: 4px;
}

#win-score {
  font-size: 16px;
  font-weight: 600;
  color: #222;
  margin-bottom: 12px;
}

#win-new-btn {
  padding: 8px 24px;
  font-size: 15px;
}

/* --- Tablet+ --- */

@media (min-width: 500px) {
  #app {
    padding: 12px;
  }

  .rack-slot {
    border-radius: 6px;
  }

  .tile-btn {
    padding: 3px;
    border-radius: 6px;
  }

  .bar-btn {
    padding: 8px 18px;
    font-size: 14px;
  }

  .app-title {
    font-size: 26px;
  }
}

@media (min-width: 768px) {
  #app {
    padding: 16px;
  }

  .rack-slot {
    border-radius: 8px;
  }

  .tile-btn {
    padding: 4px;
    border-radius: 8px;
  }

  .bar-btn {
    padding: 10px 24px;
    font-size: 16px;
  }

  #score-panel {
    width: 90px;
  }

  .score-value {
    font-size: 34px;
  }

  .score-label {
    font-size: 13px;
  }
}
