:root {
  --bg: #e8e6e0;
  --paper: #f5f4f0;
  --grid-line: #1a1a1a;
  --text: #111;
  --muted: #444;
  --cell-min: 44px;
  --safe-top: env(safe-area-inset-top, 0);
  --safe-bottom: env(safe-area-inset-bottom, 0);
  --safe-left: env(safe-area-inset-left, 0);
  --safe-right: env(safe-area-inset-right, 0);
}

* { box-sizing: border-box; }

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

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

body {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
  padding-bottom: var(--safe-bottom);
}

header {
  width: 100%;
  padding: 12px 16px;
  padding-top: max(12px, var(--safe-top));
  display: flex;
  flex-direction: column;
  gap: 10px;
  flex-shrink: 0;
}

header h1 {
  margin: 0;
  font-size: 1.25rem;
  color: var(--text);
}

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

.actions button {
  padding: 12px 14px;
  min-height: 44px;
  min-width: 44px;
  border-radius: 4px;
  border: 1px solid var(--grid-line);
  background: var(--paper);
  color: var(--text);
  font-size: 0.95rem;
  cursor: pointer;
  touch-action: manipulation;
}

.actions button:active {
  background: #e0ded8;
}

#scanBtn, #uploadBtn {
  background: var(--text);
  color: var(--paper);
  border-color: var(--text);
}

#enhancedBtn {
  background: #2d5a27;
  color: var(--paper);
  border-color: #2d5a27;
}

.status {
  padding: 10px 14px;
  background: var(--paper);
  border: 1px solid var(--grid-line);
  border-radius: 4px;
  margin: 0 16px 12px;
  min-height: 44px;
  display: flex;
  align-items: center;
  flex-shrink: 0;
}

.sheet-wrapper {
  flex: 1;
  overflow-x: auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 0 8px 16px;
  width: 100%;
  max-width: 100vw;
}

.sheet {
  width: 100%;
  border-collapse: collapse;
  background: var(--paper);
  border: 2px solid var(--grid-line);
  font-size: 14px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}

.sheet th,
.sheet td {
  border: 1px solid var(--grid-line);
  padding: 2px;
  text-align: center;
  vertical-align: middle;
}

.sheet th {
  background: #ddd9d0;
  font-weight: 700;
  padding: 6px 2px;
  font-size: 0.9em;
}

.sheet .loja-col {
  min-width: 52px;
  width: 52px;
  max-width: 52px;
  text-align: left;
  padding-left: 6px;
  font-size: 0.85em;
}

.sheet .yamp-col {
  min-width: 72px;
  width: 72px;
  background: #e8e4dc;
}

.sheet .yamp-cell.yamp-section {
  min-width: 72px;
  background: #e8e4dc;
  font-weight: 600;
}

/* YAMP column sections — match paper layout (big square, rect, bigger square, square, bigger square, gjithsej) */
.sheet .yamp-big-square { min-height: 180px; vertical-align: middle; border-left-width: 2px; }
.sheet .yamp-rect { min-height: 44px; border-top: 2px solid var(--grid-line); }
.sheet .yamp-maxmin { min-height: 88px; vertical-align: middle; border-top: 2px solid var(--grid-line); }
.sheet .yamp-summaxmin { min-height: 44px; border-top: 2px solid var(--grid-line); }
.sheet .yamp-combo { min-height: 220px; vertical-align: middle; border-top: 2px solid var(--grid-line); }
.sheet .yamp-gjithsej { min-height: 44px; border-top: 2px solid var(--grid-line); }

.sheet .dice-face-cell {
  min-height: var(--cell-min);
  padding: 4px;
  background: #f0efe8;
}
.sheet .dice-loja { font-weight: 700; font-size: 1.1em; }
.sheet .die-face { font-size: 1.25rem; display: inline-block; }

.sheet th:not(.loja-col):not(.yamp-col) {
  min-width: 44px;
  width: 44px;
}

.sheet td:not(:first-child):not(:last-child) {
  min-width: 44px;
}

.sheet input.cell-input {
  width: 100%;
  min-width: 100%;
  height: var(--cell-min);
  min-height: var(--cell-min);
  border: none;
  background: var(--paper);
  text-align: center;
  font-size: 16px;
  font-family: inherit;
}

.sheet input.cell-input:focus {
  outline: 2px solid var(--text);
  outline-offset: -2px;
}

.sheet input.cell-input.blocked {
  background: #e0ded8;
  color: var(--muted);
}

.sheet .calc-cell {
  font-weight: 600;
  min-height: var(--cell-min);
  padding: 4px;
  font-size: 13px;
}

.sheet .row-label {
  font-weight: 700;
  padding: 4px 6px;
  text-align: left;
  font-size: 0.9em;
}

.sheet .row-label.sub {
  font-size: 0.7em;
  color: var(--muted);
  font-weight: 400;
}

.dice-footer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  margin-top: 8px;
  flex-shrink: 0;
  border-top: 1px solid var(--grid-line);
}

.dice-logo-box {
  width: 36px;
  height: 36px;
  border: 1px solid var(--grid-line);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 1.25rem;
}

.dice-row {
  display: flex;
  gap: 2px;
}

.die {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  border: 1px solid var(--grid-line);
  background: var(--paper);
}

.dice-input {
  flex: 1;
  min-width: 60px;
  height: 32px;
  border: 1px solid var(--grid-line);
  background: var(--paper);
}

.overlay {
  position: fixed;
  inset: 0;
  top: var(--safe-top);
  left: var(--safe-left);
  right: var(--safe-right);
  bottom: var(--safe-bottom);
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.overlay.hidden {
  display: none;
}

.overlay-content {
  background: var(--paper);
  padding: 20px;
  border-radius: 4px;
  max-width: min(90vw, 360px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  border: 1px solid var(--grid-line);
}

#scanPreview {
  max-width: 100%;
  max-height: 50vh;
  border: 1px solid var(--grid-line);
}

#cancelScanBtn {
  padding: 12px 24px;
  min-height: 44px;
  border-radius: 4px;
  border: 1px solid var(--grid-line);
  background: var(--paper);
  color: var(--text);
  cursor: pointer;
  font-size: 1rem;
}

footer {
  margin-top: auto;
  padding: 12px;
  font-size: 0.75rem;
  color: var(--muted);
  flex-shrink: 0;
}

@media (min-width: 480px) {
  .sheet {
    font-size: 15px;
  }
  .sheet .loja-col {
    min-width: 65px;
    width: 65px;
  }
  .sheet .yamp-col,
  .sheet .yamp-cell.yamp-section {
    min-width: 90px;
    width: 90px;
  }
  .sheet th:not(.loja-col):not(.yamp-col),
  .sheet td:not(:first-child):not(:last-child) {
    min-width: 52px;
  }
}

@media (min-width: 640px) {
  .sheet {
    font-size: 16px;
  }
  .sheet .loja-col {
    min-width: 70px;
    width: 70px;
  }
  .sheet .yamp-col,
  .sheet .yamp-cell.yamp-section {
    min-width: 100px;
    width: 100px;
  }
  .sheet th:not(.loja-col):not(.yamp-col),
  .sheet td:not(:first-child):not(:last-child) {
    min-width: 60px;
  }
}

@media (min-width: 768px) {
  header {
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
  }
  .actions button {
    font-size: 1rem;
    padding: 12px 16px;
  }
}
