:root {
  --space: clamp(8px, 2vmin, 20px);
  --gap: clamp(4px, 1.2vmin, 8px);
  --green: #00ff66;
  --blue: #38bdf8;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: 'Courier New', Courier, monospace;
  -webkit-tap-highlight-color: transparent;
}

body {
  height: 100dvh;
  min-height: 100vh;
  overflow: hidden;
  background: #0d0d0d;
  color: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  user-select: none;
  -webkit-user-select: none;
}

button,
.keyboard-wrapper,
.tile {
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

.app-container {
  width: 100%;
  height: 100dvh;
  max-width: 640px;
  padding: var(--space) clamp(8px, 2.5vw, 16px);
  padding-top: calc(var(--space) + env(safe-area-inset-top, 0px));
  padding-bottom: calc(var(--space) + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(3px, 1vmin, 10px);
  overflow: hidden;
}

header {
  width: 100%;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(3px, 1.2vmin, 10px);
}

.nav-categories,
.nav-modes {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: var(--gap);
}

.tab-btn,
.btn-reroll,
.key,
.btn-primary,
.btn-secondary {
  cursor: pointer;
  font-weight: bold;
  border-radius: 6px;
}

.tab-btn {
  padding: clamp(5px, 1vmin, 8px) clamp(9px, 2vmin, 16px);
  background: #181818;
  border: 1px solid #333;
  color: #888;
  font-size: clamp(.68rem, 1.8vmin, .85rem);
}

.tab-btn.active {
  color: var(--green);
  border-color: var(--green);
}

h1 {
  color: #fff;
  font-size: clamp(1.7rem, 6vmin, 2.5rem);
  letter-spacing: 5px;
}

.subtitle {
  max-width: 100%;
  color: #888;
  font-size: clamp(.65rem, 1.8vmin, .85rem);
  text-align: center;
  white-space: nowrap;
}

.target-container {
  display: flex;
  align-items: center;
  gap: clamp(5px, 1.5vmin, 10px);
  padding: clamp(5px, 1vmin, 8px) clamp(10px, 3vmin, 20px);
  background: #141414;
  border: 1px solid #282828;
  border-radius: 6px;
}

.target-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(8px, 1.8vh, 14px);
}

.target-label,
.stat-label,
.solution-label {
  color: #aaa;
  font-size: clamp(.7rem, 1.8vmin, .95rem);
}

.target-display {
  color: var(--green);
  font-size: clamp(1.25rem, 4.5vmin, 1.8rem);
  font-weight: bold;
}

.random-action-row {
  width: 100%;
  min-height: 32px;
  display: flex;
  justify-content: center;
}

.btn-reroll {
  padding: 6px 12px;
  background: transparent;
  border: 1px dashed #2f2f2f;
  color: #707070;
  font-size: .8rem;
}

.hidden {
  display: none !important;
}

.message-banner {
  min-height: clamp(20px, 3vmin, 26px);
  flex-shrink: 0;
  font-size: clamp(.7rem, 2vmin, .95rem);
  font-weight: bold;
  text-align: center;
}

.board-wrapper {
  width: 100%;
  min-height: 0;
  flex: 1 1 auto;
  display: flex;
  justify-content: center;
  align-items: center;
}

.board-area {
  width: 100%;
  max-width: min(640px, 92vmin);
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: clamp(14px, 3.5vh, 28px);
}

.board {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(var(--board-columns, 10), minmax(0, 1fr));
  gap: var(--gap);
}

.history {
  position: relative;
  width: 100%;
  height: clamp(46px, 12vmin, 100px);
  min-height: 0;
  flex: 0 0 auto;
  overflow: hidden;
  padding-top: 2px;
  text-align: center;
}

.history::after {
  position: absolute;
  right: 0;
  bottom: 0;
  left: 0;
  height: clamp(8px, 2vmin, 14px);
  content: "";
  pointer-events: none;
  background: linear-gradient(to bottom, rgba(13, 13, 13, 0), rgba(13, 13, 13, .65) 55%, #0d0d0d);
}

.history-title {
  padding: 0 4px 5px;
  color: #aaa;
  font-size: clamp(.65rem, 1.8vmin, .8rem);
  letter-spacing: 1.5px;
  text-transform: uppercase;
}

.history-list {
  max-height: clamp(46px, 12vmin, 100px);
  overflow-y: auto;
  scrollbar-width: none;
}

.history-list::-webkit-scrollbar {
  display: none;
}

.history-entry {
  width: min(240px, 82%);
  min-height: clamp(28px, 5vmin, 38px);
  margin-left: auto;
  margin-right: auto;
  padding: 4px 8px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 20px minmax(0, 1fr);
  align-items: center;
  gap: 12px;
  background: #141414;
  border: 1px solid #333;
  border-radius: 6px;
  color: #ff7777;
  font-size: clamp(.72rem, 2vmin, .9rem);
}

.history-entry + .history-entry {
  margin-top: var(--gap);
}

.history-expression {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  justify-self: center;
  text-align: center;
}

.history-result {
  flex-shrink: 0;
  justify-self: center;
  text-align: center;
  color: #999;
}

.history-entry-correct {
  color: #ff7777;
}

.history-entry-correct .history-expression {
  color: var(--green);
}

.history-equals {
  display: block;
  justify-self: center;
  color: #aaa;
  font-weight: bold;
  text-align: center;
}

.tile {
  aspect-ratio: 1 / 1.1;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #121212;
  border: 2px solid #333;
  border-radius: 6px;
  color: #fff;
  font-size: clamp(.9rem, 4vmin, 1.5rem);
  font-weight: bold;
  transition: border-color .2s, color .2s, transform .1s;
}

.tile.filled { border-color: #888; transform: scale(1.02); }
.tile.cursor { border-color: var(--blue); box-shadow: 0 0 0 1px var(--blue); }
.tile.correct { border-color: var(--green) !important; color: var(--green) !important; }
.tile.absent { border-color: #ff4444 !important; color: #ff4444 !important; }

.keyboard-wrapper {
  width: 100%;
  max-width: 480px;
  margin-top: auto;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
}

.keyboard-row {
  display: flex;
  justify-content: center;
  gap: var(--gap);
}

.symbol-row {
  width: min(100%, 380px);
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
}

.keyboard-row.available-row { flex-wrap: wrap; }
.keyboard-row.available-row .key { flex: 0 1 64px; height: clamp(36px, 7vmin, 50px); }

.key {
  flex: 1;
  height: clamp(46px, 8.5vmin, 62px);
  background: #181818;
  border: 1px solid #303030;
  color: #fff;
  font-size: clamp(1rem, 3.5vmin, 1.3rem);
}

.key.op { color: var(--blue); }
.key.action-key { flex: 1.6; font-size: .95rem; }
.key:active { background: #2c2c2c; transform: scale(.98); }
.key.key-locked, .key:disabled { opacity: .35; cursor: not-allowed; }

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  padding: 16px;
  display: flex;
  justify-content: center;
  align-items: center;
  background: rgba(0, 0, 0, .85);
}

.modal-card {
  position: relative;
  width: 100%;
  max-width: 380px;
  padding: 28px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  background: #141414;
  border: 2px solid #333;
  border-radius: 8px;
  text-align: center;
}

.modal-close { position: absolute; top: 10px; right: 14px; background: transparent; border: 0; color: #888; font-size: 1.6rem; cursor: pointer; }
.modal-stats { display: flex; justify-content: space-around; padding: 12px; background: #0d0d0d; border: 1px solid #252525; border-radius: 6px; }
.stat-box { display: flex; flex-direction: column; gap: 4px; }
.solution-box { padding: 14px; background: #0d0d0d; border: 1px solid #252525; border-radius: 6px; }
.solution-label { display: block; margin-bottom: 4px; }
#modal-solution { color: var(--green); font-size: 1.4rem; font-weight: bold; }
.modal-actions { display: flex; flex-direction: column; gap: 10px; }
.daily-countdown { color: #888; font-size: .85rem; }
.btn-primary, .btn-secondary { padding: 12px; font-size: .9rem; }
.btn-primary { background: var(--green); border: 0; color: #000; }
.btn-secondary { background: transparent; border: 1px solid #444; color: #fff; }

.daily-date-nav {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #141414;
  border: 1px solid #282828;
  border-radius: 6px;
  padding: 4px 10px;
}

.date-nav-btn {
  background: transparent;
  border: 0;
  color: var(--green);
  font-size: 0.9rem;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
}

.date-nav-btn:disabled {
  color: #444;
  cursor: not-allowed;
}

.date-nav-label {
  background: transparent;
  border: 0;
  color: #fff;
  font-size: 0.85rem;
  font-weight: bold;
  cursor: pointer;
  padding: 2px 6px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.date-nav-label:hover {
  color: var(--green);
}

.calendar-modal-card {
  max-width: 360px;
  padding: 20px;
  gap: 14px;
}

.calendar-month-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: bold;
  color: var(--green);
  font-size: 0.95rem;
  min-height: 36px;
}

#calendar-month-year {
  flex: 1;
  text-align: center;
}

.calendar-nav-btn {
  background: #222;
  border: 1px solid #333;
  color: #fff;
  padding: 4px 12px;
  border-radius: 4px;
  cursor: pointer;
}

.calendar-nav-btn:hover {
  border-color: var(--green);
}

.calendar-weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  font-size: 0.75rem;
  color: #777;
  text-transform: uppercase;
}

.calendar-days-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  grid-template-rows: repeat(6, 1fr);
  gap: 4px;
}

.calendar-day {
  aspect-ratio: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: #1a1a1a;
  border: 1px solid #333;
  border-radius: 4px;
  font-size: 0.85rem;
  color: #ccc;
  cursor: pointer;
  position: relative;
  box-sizing: border-box;
}

.calendar-day.empty {
  background: transparent !important;
  border-color: transparent !important;
  pointer-events: none;
}

.calendar-day:hover:not(.disabled) {
  border-color: #fff;
  color: #fff;
}

.calendar-day.completed {
  border-color: var(--green) !important;
  color: var(--green) !important;
  font-weight: bold;
}

.calendar-day.in-progress {
  border-color: #ffaa00 !important;
  color: #ffaa00 !important;
  font-weight: bold;
}

.calendar-day.selected {
  box-shadow: 0 0 0 2px var(--blue);
}

.calendar-day.disabled {
  opacity: 0.25;
  cursor: not-allowed;
  background: #0d0d0d;
}

.calendar-actions {
  margin-top: 6px;
  display: flex;
  justify-content: center;
}

@media (max-width: 480px) {
  .app-container {
    padding-right: clamp(6px, 2.5vw, 12px);
    padding-left: clamp(6px, 2.5vw, 12px);
    gap: 4px;
  }

  header {
    gap: 6px;
  }

  h1 {
    font-size: clamp(1.6rem, 5.5vmin, 2.2rem);
    letter-spacing: 3px;
  }

  .subtitle {
    white-space: normal;
    font-size: clamp(.65rem, 1.7vmin, .78rem);
    line-height: 1.2;
  }

  .nav-categories,
  .nav-modes {
    gap: 4px;
  }

  .tab-btn {
    padding: 5px 10px;
    font-size: 0.72rem;
  }

  .board {
    width: min(100%, 380px);
    margin: 0 auto;
    grid-template-columns: repeat(var(--board-columns, 10), minmax(0, 1fr));
    gap: clamp(2px, 1vw, 5px);
  }

  .board-area {
    gap: clamp(8px, 2vh, 16px);
  }

  .tile {
    min-width: 0;
    font-size: clamp(.85rem, 4.2vw, 1.25rem);
  }

  .keyboard-wrapper {
    max-width: 420px;
    gap: 4px;
  }

  .keyboard-row.available-row .key {
    flex: 0 1 clamp(28px, 8.5vw, 44px);
    min-height: 38px;
    height: clamp(38px, 10vw, 46px);
    font-size: clamp(0.9rem, 3vmin, 1.15rem);
  }

  .key {
    min-height: 42px;
    height: clamp(42px, 12vw, 54px);
    font-size: clamp(0.9rem, 3.2vmin, 1.15rem);
  }

  .key.action-key {
    font-size: 0.85rem;
  }
}

/* Short height screens (e.g. iPhone SE, height <= 720px) */
@media (max-height: 720px) {
  :root {
    --space: 4px;
    --gap: 3px;
  }

  h1 {
    font-size: 1.5rem;
    letter-spacing: 2px;
  }

  .target-wrapper {
    gap: 4px;
  }

  .random-action-row {
    min-height: 24px;
  }

  .btn-reroll {
    padding: 3px 8px;
    font-size: 0.75rem;
  }

  .board-area {
    gap: 8px;
  }

  .history {
    height: 42px;
  }

  .history-list {
    max-height: 42px;
  }

  .history-entry {
    min-height: 24px;
    padding: 2px 6px;
    font-size: 0.72rem;
  }

  .key {
    min-height: 38px;
    height: clamp(38px, 5.5vmin, 46px);
    font-size: 0.95rem;
  }

  .keyboard-row.available-row .key {
    min-height: 34px;
    height: 38px;
  }

  .key.action-key {
    font-size: 0.8rem;
  }
}

/* Very compact / landscape mode (height <= 560px) */
@media (max-height: 560px) {
  .app-container {
    overflow-y: auto;
    justify-content: flex-start;
    padding-top: 4px;
    padding-bottom: 4px;
  }

  h1, .subtitle, .history {
    display: none;
  }
}
