/* ==========================================================================
   SECTION A: GLOBAL THEME AND RESET
   ========================================================================== */

:root {
  --blue: #2563eb;
  --blue-hover: #1d4ed8;
  --blue-light: #dbeafe;
  --blue-hint-active: #38bdf8;
  --green: #16a34a;
  --green-light: #bbf7d0;
  --red: #dc2626;
  --red-light: #fee2e2;
  --amber: #d97706;
  --amber-bg: #fef3c7;
  --purple: #7c3aed;
  --purple-hover: #6d28d9;
  --text: #0f172a;
  --muted: #475569;
  --border: #cbd5e1;
  --card: #ffffff;

  --grid-size: 6;
  --cell-gap: 8px;

  /*
    These values are recalculated by game.js according to the actual rendered
    tile size. Larger tiles receive larger, heavier characters. Smaller tiles
    receive smaller, lighter characters.
  */
  --tile-font-size: 42px;
  --tile-font-weight: 500;

  --font-system:
    system-ui,
    -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    Roboto,
    "Helvetica Neue",
    Arial,
    "PingFang SC",
    "Hiragino Sans GB",
    "Microsoft YaHei",
    "WenQuanYi Micro Hei",
    "Noto Sans CJK SC",
    "PingFang TC",
    "Microsoft JhengHei",
    "Noto Sans CJK TC",
    sans-serif;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: #f8fafc;
  color: var(--text);
  font-family: var(--font-system);
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* ==========================================================================
   SECTION B: BUTTONS AND FOCUS STYLES
   ========================================================================== */

button {
  appearance: none;
  -webkit-appearance: none;
  border: 0;
  border-radius: 10px;
  padding: 10px 18px;
  background: var(--text);
  color: #ffffff;
  font: 600 0.95rem/1.2 var(--font-system);
  cursor: pointer;

  transition:
    background-color 140ms ease,
    color 140ms ease,
    border-color 140ms ease,
    transform 100ms ease,
    opacity 140ms ease;
}

button:hover:not(:disabled) {
  opacity: 0.94;
  transform: translateY(-1px);
}

button:active:not(:disabled) {
  transform: translateY(0);
}

button:focus-visible,
.cell:focus-visible,
.cal-day:focus-visible {
  outline: 3px solid rgba(37, 99, 235, 0.35);
  outline-offset: 2px;
}

button:disabled {
  opacity: 0.42;
  cursor: not-allowed;
}

button.secondary {
  background: #e2e8f0;
  color: var(--text);
}

button.secondary:hover:not(:disabled) {
  background: #cbd5e1;
}

button.blue {
  background: var(--blue);
  color: #ffffff;
}

button.blue:hover:not(:disabled) {
  background: var(--blue-hover);
}

button.purple {
  background: var(--purple);
  color: #ffffff;
}

button.purple:hover:not(:disabled) {
  background: var(--purple-hover);
}

/* ==========================================================================
   SECTION C: MAIN GAME LAYOUT
   ========================================================================== */

.game {
  position: relative;
  width: min(96vw, 820px);
  margin: auto;

  padding:
    max(16px, env(safe-area-inset-top))
    max(12px, env(safe-area-inset-right))
    max(36px, env(safe-area-inset-bottom))
    max(12px, env(safe-area-inset-left));

  text-align: center;
}

.top-actions {
  position: absolute;
  top: max(14px, env(safe-area-inset-top));
  right: max(14px, env(safe-area-inset-right));
  z-index: 10;

  display: flex;
  align-items: center;
  gap: 8px;
}

.btn-top-new {
  min-height: 38px;
  padding: 8px 12px;
  border-radius: 10px;
  background: #e2e8f0;
  color: var(--text);
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.08);
}

.btn-help {
  display: flex;
  width: 38px;
  height: 38px;
  align-items: center;
  justify-content: center;
  padding: 0;
  border-radius: 50%;
  background: #e2e8f0;
  color: var(--text);
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.08);
  font-size: 1.25rem;
  font-weight: 700;
}

h1 {
  margin: 0 0 10px;
  padding: 0 180px;
  color: #0f172a;
  font-size: clamp(1.8rem, 6vw, 2.9rem);
  font-weight: 700;
  letter-spacing: 0.08em;
}

.controls,
.stats {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.controls {
  margin-bottom: 14px;
}

.controls button {
  padding: 8px 16px;
}

.stats {
  margin-bottom: 12px;
}

.stat {
  min-width: 140px;
  padding: 9px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #ffffff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.04);
  font-size: 1.08rem;
  font-weight: 500;
}

.stat strong {
  color: var(--blue);
  font-size: 1.15rem;
  font-variant-numeric: tabular-nums;
}

#timer.time-warning {
  color: var(--red);
  font-weight: 800;
}

#timer.time-flashing {
  color: var(--red);
  animation: timerFlash 0.6s infinite alternate;
}

.instructions {
  min-height: 44px;
  margin: 0 auto 8px;
  color: var(--muted);
  font-size: 1.05rem;
  font-weight: 500;
  line-height: 1.45;
}

.hint-row-top {
  display: flex;
  min-height: 42px;
  align-items: center;
  justify-content: center;
  margin: 0 auto 10px;
}

.play-area {
  transition: filter 220ms ease;
}

.play-area.blurred {
  filter: blur(8px);
  pointer-events: none;
}

/* ==========================================================================
   SECTION D: GAME GRID AND TILES
   ========================================================================== */

.grid-container {
  width: min(94vw, 620px);
  margin: 0 auto;
}

.grid {
  display: grid;
  width: 100%;
  aspect-ratio: 1 / 1;

  grid-template-columns:
    repeat(var(--grid-size), minmax(0, 1fr));

  grid-template-rows:
    repeat(var(--grid-size), minmax(0, 1fr));

  gap: var(--cell-gap);
  touch-action: none;
}

.cell {
  display: flex;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  align-items: center;
  justify-content: center;

  border: 1.5px solid var(--border);
  border-radius: 12px;
  background: #ffffff;
  color: var(--text);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.03);
  cursor: pointer;

  font-family: var(--font-system);
  font-size: var(--tile-font-size);
  font-weight: var(--tile-font-weight);
  line-height: 1;

  transition:
    background-color 120ms ease,
    border-color 120ms ease,
    color 120ms ease,
    transform 120ms ease,
    opacity 300ms ease;
}

.cell.selected {
  border-color: var(--blue);
  background: var(--blue);
  color: #ffffff;
  box-shadow: 0 4px 10px rgba(37, 99, 235, 0.35);
  transform: scale(1.05);
}

.cell.hint {
  border-color: #f59e0b;
  background: var(--amber-bg);
  color: #92400e;
  animation: hintPulse 1.2s infinite;
}

.cell.hint.selected {
  border-color: var(--blue);
  background: var(--blue-hint-active);
  color: #ffffff;
  animation: none;
}

.cell.success {
  border-color: var(--green);
  background: var(--green-light);
  color: #166534;
  animation: successPop 420ms ease;
}

.cell.error {
  border-color: var(--red);
  background: var(--red-light);
  animation: errorShake 300ms ease;
}

.cell.removed {
  opacity: 0;
  pointer-events: none;
  transform: scale(0.35);
}

.message {
  min-height: 32px;
  margin-top: 12px;
  color: var(--green);
  font-size: 1.15rem;
  font-weight: 700;
}

.message.error {
  color: var(--red);
}

.message.hint-msg {
  color: var(--amber);
}

/* ==========================================================================
   SECTION E: MODALS
   ========================================================================== */

.modal-layer {
  position: fixed;
  inset: 0;
  z-index: 30;

  display: flex;
  overflow: auto;
  align-items: center;
  justify-content: center;

  padding:
    max(16px, env(safe-area-inset-top))
    max(16px, env(safe-area-inset-right))
    max(16px, env(safe-area-inset-bottom))
    max(16px, env(safe-area-inset-left));

  background: rgba(15, 23, 42, 0.65);
  -webkit-backdrop-filter: blur(5px);
  backdrop-filter: blur(5px);
}

#tutorialOverlay {
  z-index: 45;
}

#infoOverlay {
  z-index: 50;
}

.modal {
  width: min(95vw, 560px);
  max-height: 94vh;
  max-height: 94dvh;
  overflow: auto;

  padding: 26px 24px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--card);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.28);

  text-align: center;
  user-select: text;
  -webkit-user-select: text;
  -webkit-overflow-scrolling: touch;
}

.modal-results {
  display: flex;
  width: min(95vw, 840px);
  max-height: 92vh;
  max-height: 92dvh;
  flex-direction: column;
  overflow: hidden;
}

.modal h2 {
  margin: 0 0 12px;
  color: #0f172a;
  font-size: 1.8rem;
}

.modal p {
  color: var(--muted);
  font-size: 1.05rem;
  line-height: 1.6;
}

.modal-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 12px;
  margin-top: 20px;
  user-select: none;
  -webkit-user-select: none;
}

.options-grid {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin: 20px 0;
}

.options-grid button {
  padding: 14px 18px;
  border-radius: 12px;
  font-size: 1.1rem;
}

.info-modal-text {
  text-align: left;
}

/* ==========================================================================
   SECTION F: CALENDAR
   ========================================================================== */

.calendar-container {
  width: 100%;
  max-width: 380px;
  margin: 16px auto;
  padding: 14px;
  border: 1.5px solid var(--border);
  border-radius: 14px;
  background: #ffffff;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  user-select: none;
  -webkit-user-select: none;
}

.calendar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.calendar-header button {
  padding: 6px 14px;
  border-radius: 8px;
  background: #f1f5f9;
  color: var(--text);
  font-size: 1.2rem;
}

.calendar-header span {
  color: #0f172a;
  font-size: 1.15rem;
  font-weight: 700;
}

.calendar-weekdays,
.calendar-days {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
}

.calendar-weekdays {
  gap: 4px;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 700;
}

.calendar-days {
  gap: 5px;
}

.cal-day {
  display: flex;
  aspect-ratio: 1 / 1;
  align-items: center;
  justify-content: center;

  padding: 0;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  background: #f8fafc;
  color: var(--text);

  cursor: pointer;
  font-family: inherit;
  font-size: 1rem;
  font-weight: 600;
  opacity: 1;

  transition:
    background-color 120ms ease,
    border-color 120ms ease,
    color 120ms ease,
    transform 120ms ease;
}

.cal-day:hover:not(:disabled) {
  border-color: #93c5fd;
  background: var(--blue-light);
  color: var(--blue);
  opacity: 1;
  transform: translateY(-1px);
}

.cal-day:active:not(:disabled) {
  transform: translateY(0);
}

.cal-day.selected {
  border-color: var(--blue) !important;
  background: var(--blue) !important;
  color: #ffffff !important;
  font-weight: 700;
  opacity: 1;
}

.cal-day.today:not(.selected) {
  border: 2px solid var(--blue);
  background: #eff6ff;
  color: var(--blue);
  font-weight: 700;
}

.cal-day.disabled,
.cal-day:disabled {
  border-color: transparent;
  background: #f1f5f9;
  color: #94a3b8;
  cursor: not-allowed;
  opacity: 0.55;
  transform: none;
}

.cal-day.empty {
  border-color: transparent;
  background: transparent;
  color: transparent;
  cursor: default;
  pointer-events: none;
}

.date-display-badge {
  margin-top: 10px;
  color: var(--blue);
  font-size: 1.15rem;
  font-weight: 700;
}

/* ==========================================================================
   SECTION G: TUTORIAL
   ========================================================================== */

.tutorial-step {
  min-height: 130px;
}

.tutorial-step .big {
  display: block;
  margin: 12px 0;
  color: var(--blue);
  font-size: clamp(1.7rem, 7vw, 2.3rem);
  font-weight: 700;
  letter-spacing: 0.08em;
}

/* ==========================================================================
   SECTION H: RESULTS
   ========================================================================== */

.results {
  min-height: 0;
  max-height: 68vh;
  overflow-y: auto;
  overscroll-behavior: contain;

  margin-top: 14px;
  padding-right: 8px;
  text-align: left;

  user-select: text;
  -webkit-user-select: text;
  -webkit-overflow-scrolling: touch;
}

.result-item {
  margin: 12px 0;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: #fdfdfd;
  font-size: 1.05rem;
  line-height: 1.65;
}

.result-item.solved {
  border-color: #5dd55d;
  background: #f0fdf4;
}

.result-item.hinted {
  border-color: #fed7aa;
  background: #fff7ed;
}

.result-item.missed {
  border-color: #fecaca;
  background: #fef2f2;
}

.result-header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 10px;
  margin-bottom: 6px;
}

/* OLD:
.result-header {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 6px;
}
*/

.result-primary-line strong {
  color: #0f172a;
  font-size: 1.6rem;
}

.result-identity-block {
  display: flex;
  min-width: 0;
  flex: 1 1 300px;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
}

.result-primary-line {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 10px;
}

.result-primary-line strong {
  color: #0f172a;
  font-size: 1.6rem;
}

.result-alternate-form {
  color: #64748b;
  font-size: 0.9rem;
  font-weight: 500;
  line-height: 1.35;
}
.result-phonetics {
  color: var(--blue);
  font-size: 1.05rem;
  font-weight: 600;
}

.zhuyin-tag {
  margin-left: 4px;
  color: #9333ea;
  font-size: 0.95rem;
}

.result-def {
  margin: 6px 0;
  color: #334155;
}

/*
  Character and character-group explanations appear between each idiom's
  definition and its example sentence.
*/
.result-char-breakdown {
  margin: 10px 0 12px;
  padding: 10px 12px;
  border-left: 4px solid #93c5fd;
  border-radius: 0 10px 10px 0;
  background: #f8fafc;
}

.char-note {
  margin: 4px 0;
  color: #334155;
  font-weight: 500;
}

.char-note-en {
  margin: 0 0 8px 16px;
  color: #64748b;
  font-size: 0.95rem;
}

.char-note-en:last-child {
  margin-bottom: 0;
}

.result-ex {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px dashed #e2e8f0;
  color: #475569;
  font-size: 0.98rem;
}

.result-ex em {
  color: var(--blue);
  font-style: normal;
  font-weight: 700;
}

.result-status {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  align-self: flex-start;
  gap: 2px;
  margin-left: auto;
  padding: 5px 9px;
  border-radius: 7px;
  text-align: right;
  line-height: 1.3;
}

.result-status-text {
  font-size: 0.82rem;
  font-weight: 700;
}

.result-status-points {
  font-size: 0.8rem;
  font-weight: 700;
}

/* Solved without hints */
.result-status.solved {
  background: #dcfce7;
  color: #15803d;
}

/* Solved after one to three characters were revealed */
.result-status.hinted {
  background: #fef3c7;
  color: #b45309;
}

/* Automatically completed by the fourth hint */
.result-status.revealed {
  background: #e2e8f0;
  color: #475569;
}

/* Not found before the game ended */
.result-status.missed {
  background: #fee2e2;
  color: #b91c1c;
}

@media (max-width: 480px) {
  .result-status {
    width: 100%;
    align-items: flex-start;
    margin-top: 6px;
    margin-left: 0;
    text-align: left;
  }
}

.item-links-row {
  margin-top: 10px;
}

.dotted-link {
  display: inline-block;
  margin: 3px 12px 3px 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: var(--blue);
  font-size: 0.95rem;
  font-weight: 600;
  text-decoration: underline dotted;
}

.dotted-link:hover:not(:disabled) {
  background: transparent;
  color: var(--blue-hover);
  opacity: 1;
  transform: none;
}

.daily-highlights {
  margin-top: 24px;
  padding-top: 16px;
  border-top: 2px dashed var(--border);
}

.daily-highlights h3 {
  margin-bottom: 8px;
  color: var(--blue);
  font-size: 1.35rem;
}

.highlight-orig {
  margin-bottom: 6px;
  color: #1e293b;
  font-size: 1.15rem;
  font-weight: 500;
}

.highlight-trans {
  margin-top: 4px;
  color: #475569;
  font-size: 1rem;
}

.story-image-container {
  margin: 16px 0;
  text-align: center;
}

.story-image-container img {
  max-width: 100%;
  max-height: 340px;
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.08);
  object-fit: contain;
}

/* ==========================================================================
   SECTION I: CELEBRATION
   ========================================================================== */

.celebration {
  position: fixed;
  inset: 0;
  z-index: 100;
  overflow: hidden;
  pointer-events: none;
}

.confetti-piece {
  position: absolute;
  top: -24px;
  width: 10px;
  height: 18px;
  border-radius: 2px;
  opacity: 0;

  animation:
    confettiFall var(--duration)
    cubic-bezier(0.12, 0.72, 0.28, 1)
    forwards;

  animation-delay: var(--delay);
}

/* ==========================================================================
   SECTION J: ANIMATIONS
   ========================================================================== */

@keyframes timerFlash {
  from {
    opacity: 1;
    transform: scale(1);
  }

  to {
    opacity: 0.2;
    transform: scale(1.08);
  }
}

@keyframes successPop {
  0% {
    transform: scale(1);
  }

  45% {
    transform: scale(1.15) rotate(2deg);
  }

  100% {
    transform: scale(1);
  }
}

@keyframes errorShake {
  0%,
  100% {
    transform: translateX(0);
  }

  25% {
    transform: translateX(-6px);
  }

  75% {
    transform: translateX(6px);
  }
}

@keyframes hintPulse {
  0%,
  100% {
    box-shadow: 0 0 0 0 rgba(245, 158, 11, 0.45);
  }

  50% {
    box-shadow: 0 0 0 8px rgba(245, 158, 11, 0);
  }
}

@keyframes confettiFall {
  0% {
    opacity: 1;
    transform:
      translate3d(0, -20px, 0)
      rotate(0deg);
  }

  100% {
    opacity: 0.95;
    transform:
      translate3d(var(--drift), 110vh, 0)
      rotate(var(--rotation));
  }
}

/* ==========================================================================
   SECTION K: RESPONSIVE LAYOUT
   ========================================================================== */

@media (max-width: 650px) {
  h1 {
    padding: 46px 0 0;
  }

  .top-actions {
    left: 12px;
    right: 12px;
    justify-content: flex-end;
  }

  .stat {
    min-width: min(44vw, 170px);
  }

  .modal {
    padding: 22px 16px;
  }
}

@media (max-width: 480px) {
  :root {
    --cell-gap: 5px;
  }

  .game {
    width: 100%;
  }

  .controls {
    gap: 6px;
  }

  .controls button {
    padding: 8px 10px;
    font-size: 0.88rem;
  }

  .stat {
    min-width: 0;
    width: calc(50% - 6px);
    padding: 8px;
    font-size: 0.94rem;
  }

  .stat:last-child {
    width: 52%;
  }

  .cell {
    border-radius: 8px;
  }

  .result-item {
    padding: 13px;
  }

  .result-char-breakdown {
    padding: 9px 10px;
  }
}

@media (max-width: 380px) {
  :root {
    --cell-gap: 4px;
  }

  .cell {
    border-radius: 7px;
  }
}

/* ==========================================================================
   SECTION L: REDUCED MOTION
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}
