* {
  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;
}

/* --- Rack --- */

#rack {
  display: grid;
  grid-template-columns: repeat(14, minmax(0, 1fr));
  gap: 3px;
  flex-shrink: 0;
  background: rgba(255, 255, 255, 0.45);
  border-radius: 8px;
  padding: 6px 8px;
}

.rack-slot {
  aspect-ratio: 3 / 4;
  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;
}

/* --- 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-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-shrink: 0;
  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;
}

.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-shrink: 0;
  background: rgba(255, 255, 255, 0.45);
  border-radius: 8px;
  padding: 6px 8px;
}

.tile-row-p {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 3px;
}

/* --- 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;
}

.version-text {
  font-size: 11px;
  color: #888;
}

.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: buttons in empty 2-column space right of tiles --- */

@media (max-height: 500px) and (orientation: landscape) {
  #app {
    position: relative;
  }

  #button-bar {
    position: absolute;
    right: 8px;
    top: calc(8px + (100vw - 16px - 39px) / 14 * 4 / 3 + 30px);
    width: calc(1.5 / 14 * (100vw - 16px));
    flex-direction: column;
    align-items: stretch;
    gap: 3px;
    padding: 0;
  }

  .bar-left, .bar-right {
    flex-direction: column;
    gap: 3px;
  }

  .bar-center {
    display: none;
  }

  .bar-btn {
    padding: 4px 0;
    font-size: 10px;
    text-align: center;
  }
}

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

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

  #rack {
    grid-template-columns: repeat(7, 1fr);
  }

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

  #tile-rows-portrait {
    display: flex;
  }

  #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;
  }
}

/* --- 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;
  }

  .version-text {
    font-size: 12px;
  }
}

@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;
  }
}

/* --- Match Page --- */

#match-page {
  display: none;
  position: fixed;
  inset: 0;
  background: rgb(221, 226, 219);
  z-index: 100;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px;
}

#match-page.visible {
  display: block;
}

#match-close {
  position: sticky;
  top: 0;
  float: right;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  font-size: 22px;
  line-height: 36px;
  text-align: center;
  cursor: pointer;
  z-index: 101;
  margin: 0 0 -36px;
}

#match-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-width: 900px;
  margin: 0 auto;
}

.match-row {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(255, 255, 255, 0.45);
  border-radius: 8px;
  padding: 6px 8px;
}

.match-info {
  flex-shrink: 0;
  min-width: 0;
  width: 160px;
}

.match-text {
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.match-note {
  font-size: 11px;
  color: #666;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.match-count {
  font-size: 11px;
  color: #444;
  margin-top: 1px;
}

.match-tiles {
  display: flex;
  gap: 2px;
  flex: 1;
  min-width: 0;
}

.match-tile {
  flex: 1;
  min-width: 0;
  aspect-ratio: 3 / 4;
  border-radius: 4px;
  background: #fff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12);
  overflow: hidden;
}

.match-tile img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.match-tile.tile-dimmed {
  opacity: 0.3;
  background: #bbb;
}

.match-count-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 4px;
}

.all-hands-btn {
  padding: 2px 6px;
  border: 1px solid #ccc;
  border-radius: 4px;
  background: #fff;
  font-size: 10px;
  color: #333;
  cursor: pointer;
  white-space: nowrap;
}

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

@media (max-width: 600px) {
  .match-info {
    width: 120px;
  }
  .match-text {
    font-size: 11px;
  }
  .match-note {
    font-size: 10px;
  }
  .match-count {
    font-size: 10px;
  }
  .match-tile {
    border-radius: 3px;
  }
}

@media (max-width: 600px) and (orientation: portrait) {
  .match-row {
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
  }
  .match-info {
    width: auto;
  }
  .match-tiles {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 2px;
  }
}

/* --- All Hands Page --- */

#allhands-page {
  display: none;
  position: fixed;
  inset: 0;
  background: rgb(221, 226, 219);
  z-index: 102;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px;
}

#allhands-page.visible {
  display: block;
}

#allhands-close {
  position: sticky;
  top: 0;
  float: right;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  font-size: 22px;
  line-height: 36px;
  text-align: center;
  cursor: pointer;
  z-index: 103;
  margin: 0 0 -36px;
}

#allhands-header {
  max-width: 900px;
  margin: 0 auto 8px;
  background: rgba(255, 255, 255, 0.45);
  border-radius: 8px;
  padding: 8px 12px;
}

#allhands-header .match-text {
  font-size: 15px;
  font-weight: 600;
}

#allhands-header .match-note {
  font-size: 12px;
  color: #666;
  margin-top: 2px;
}

#allhands-header .match-count {
  float: right;
  font-size: 15px;
  margin-right: 36px;
}

#allhands-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-width: 900px;
  margin: 0 auto;
  background: rgba(255, 255, 255, 0.45);
  border-radius: 8px;
  padding: 6px 8px;
}

.allhands-row {
  display: flex;
  gap: 2px;
  padding-right: 168px;
}

@media (max-width: 600px) and (orientation: portrait) {
  .allhands-row {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 2px;
    padding-right: 0;
    padding-bottom: 6px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.08);
  }
  .allhands-row:last-child {
    padding-bottom: 0;
    border-bottom: none;
  }
}

/* --- Help Page --- */

#help-page {
  display: none;
  position: fixed;
  inset: 0;
  background: rgb(221, 226, 219);
  z-index: 100;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px;
}

#help-page.visible {
  display: block;
}

#help-close {
  position: sticky;
  top: 0;
  float: right;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  font-size: 22px;
  line-height: 36px;
  text-align: center;
  cursor: pointer;
  z-index: 101;
  margin: 0 0 -36px;
}

#help-content {
  max-width: 500px;
  margin: 0 auto;
  padding: 16px;
  background: rgba(255, 255, 255, 0.45);
  border-radius: 8px;
}

#help-content h2 {
  margin: 0 0 12px;
  font-size: 20px;
}

#help-content h3 {
  margin: 14px 0 6px;
  font-size: 16px;
}

#help-content p {
  margin: 0 0 8px;
  font-size: 14px;
  line-height: 1.5;
  color: #333;
}

