/* === Reset & Base === */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --color-bg: #121213;
  --color-text: #ffffff;
  --color-tile-border: #3a3a3c;
  --color-tile-filled: #565758;
  --color-correct: #6aaa64;
  --color-present: #c9b458;
  --color-close: #5b85c7;
  --color-absent: #3a3a3c;
  --color-key-bg: #818384;
  --color-key-text: #ffffff;
  --color-header-border: #3a3a3c;
  --tile-size: clamp(40px, calc((100vw - 48px) / var(--word-length, 5)), 56px);
  --tile-gap: 4px;
  --key-height: 32px;
  --keyboard-gap: 8px; /* Adjust this value to change spacing between keyboard keys */
}

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  display: flex;
  justify-content: center;
  height: 100dvh;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: 'Hiragino Kaku Gothic ProN', 'Noto Sans JP', sans-serif;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
  overflow: hidden;
}

.app-container {
  position: relative;
  width: 100%;
  max-width: 550px;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* === Floating Header Buttons === */
.header-btn {
  position: absolute;
  top: 12px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(70, 70, 75, 0.85);
  border: none;
  color: var(--color-text);
  font-size: 1.1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

#btn-help     { left: 12px; }
#btn-stats    { right: 56px; }
#btn-new-game { right: 12px; }

.header-btn:active {
  background: rgba(120, 120, 125, 0.9);
}

/* === Main / Grid === */
.main {
  flex: 1 1 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 4px 8px;
  transition: transform 0.25s ease;
  -webkit-overflow-scrolling: touch;
}

.main.grid-shifted {
  transform: translateY(40px);
}

.length-indicator {
  font-size: 0.85rem;
  color: #888;
  margin-bottom: 8px;
}

.grid-container {
  display: grid;
  grid-template-columns: repeat(var(--word-length, 5), var(--tile-size));
  grid-auto-rows: var(--tile-size);
  gap: var(--tile-gap);
}

/* === Tiles === */
.tile {
  width: var(--tile-size);
  height: var(--tile-size);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--tile-size) * 0.55);
  font-weight: 700;
  border: 2px solid var(--color-tile-border);
  background: transparent;
  color: var(--color-text);
  transition: border-color 0.1s;
}

.tile.filled {
  border-color: var(--color-tile-filled);
  animation: pop 0.1s ease-in-out;
}

.tile.active {
  border-width: 3px;
  border-color: #ccc;
}

.tile.input-row {
  cursor: pointer;
}

.tile--correct {
  background: var(--color-correct) !important;
  border-color: var(--color-correct) !important;
}

.tile--present {
  background: var(--color-present) !important;
  border-color: var(--color-present) !important;
}

.tile--close {
  background: var(--color-close) !important;
  border-color: var(--color-close) !important;
}

.tile--absent {
  background: var(--color-absent) !important;
  border-color: var(--color-absent) !important;
}

/* Flip animation */
.tile.revealing {
  animation: flip-out 0.2s ease-in forwards;
}

.tile.revealed {
  animation: flip-in 0.2s ease-out forwards;
}

@keyframes flip-out {
  0% { transform: rotateX(0deg); }
  100% { transform: rotateX(90deg); }
}

@keyframes flip-in {
  0% { transform: rotateX(90deg); }
  100% { transform: rotateX(0deg); }
}

@keyframes pop {
  0% { transform: scale(1); }
  50% { transform: scale(1.1); }
  100% { transform: scale(1); }
}

/* Shake animation */
.tile.shake {
  animation: shake 0.4s ease-in-out;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-5px); }
  40% { transform: translateX(5px); }
  60% { transform: translateX(-5px); }
  80% { transform: translateX(5px); }
}

/* Bounce animation */
.tile.bounce {
  animation: bounce 0.5s ease;
}

@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  40% { transform: translateY(-20px); }
  60% { transform: translateY(-10px); }
}

/* === Flick Keyboard === */
.keyboard-container {
  flex: 0 0 auto;
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
  position: relative;
  overflow: hidden;
  transition: height 0.25s ease;
}

.flick-keyboard {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, auto);
  gap: var(--keyboard-gap);
  padding: 6px;
  padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 16px);
  transition: transform 0.25s ease, opacity 0.25s ease;
}

.keyboard-hidden {
  transform: translateY(100%);
  opacity: 0;
  pointer-events: none;
}

.keyboard-visible {
  transform: translateY(0);
  opacity: 1;
  pointer-events: auto;
}

.flick-key {
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-key-bg);
  color: var(--color-key-text);
  border: none;
  border-radius: 6px;
  font-size: 27px;
  font-weight: 400;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background-color 0.2s;
  font-family: inherit;
  padding: 0;
  touch-action: none;
}

.flick-key:active,
.flick-key--active {
  background: #555;
  transform: scale(0.95);
}

.flick-key--backspace:active,
.flick-key--enter:active,
.flick-key--dakuten:active {
  background: #3a3a3a;
}

.flick-key--backspace {
  font-size: 22px;
  background: #555;
}

.flick-key--enter {
  font-size: 17px;
  font-weight: 400;
  background: #555;
  letter-spacing: 0.05em;
  height: auto;
}

.flick-key--dakuten {
  font-size: 16px;
  background: #555;
}

.flick-key--empty {
  background: transparent;
  border: none;
}

/* === Flick Popup === */
.flick-popup {
  position: fixed;
  width: 150px;
  height: 150px;
  z-index: 200;
  pointer-events: none;
  background: #2a2a3c;
  border-radius: 20px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.6);
  transform: translate(-50%, -50%);
}

.flick-popup.hidden {
  display: none;
}

.flick-popup__label {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 22px;
  font-weight: 700;
  transform: translate(-50%, -50%);
}

/* Chars placed in a cross pattern inside the rounded square */
.flick-popup__label--center { top: 50%; left: 50%; }
.flick-popup__label--up     { top: 15%; left: 50%; }
.flick-popup__label--down   { top: 85%; left: 50%; }
.flick-popup__label--left   { top: 50%; left: 15%; }
.flick-popup__label--right  { top: 50%; left: 85%; }

.flick-popup__label--active {
  color: var(--color-correct);
  transform: translate(-50%, -50%) scale(1.2);
}

.flick-popup__label--empty {
  opacity: 0;
}

/* === Keyboard Toggle Button === */
.keyboard-toggle {
  position: absolute;
  bottom: 16px;
  right: 16px;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: var(--color-key-bg);
  color: var(--color-key-text);
  border: none;
  font-size: 24px;
  cursor: pointer;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
  -webkit-tap-highlight-color: transparent;
}

.keyboard-toggle:active {
  background: #999;
}

/* === Toast === */
.toast {
  position: fixed;
  top: 60px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--color-text);
  color: var(--color-bg);
  padding: 10px 20px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 700;
  z-index: 100;
  opacity: 1;
  transition: opacity 0.3s;
}

.toast.hidden {
  opacity: 0;
  pointer-events: none;
}

/* === Modal === */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}

.modal-overlay.hidden {
  display: none;
}

.modal-content {
  background: #1a1a1b;
  border-radius: 12px;
  padding: 24px;
  max-width: 90vw;
  width: 340px;
  max-height: 80vh;
  overflow-y: auto;
  text-align: center;
}

.modal-title {
  font-size: 1.2rem;
  font-weight: 700;
  margin-bottom: 16px;
}

.modal-close {
  position: absolute;
  top: 8px;
  right: 12px;
  background: none;
  border: none;
  color: var(--color-text);
  font-size: 1.5rem;
  cursor: pointer;
}

/* Stats */
.stats-row {
  display: flex;
  justify-content: center;
  gap: 16px;
  margin-bottom: 20px;
}

.stat-item {
  text-align: center;
}

.stat-value {
  font-size: 1.8rem;
  font-weight: 700;
}

.stat-label {
  font-size: 0.7rem;
  color: #888;
}

.distribution {
  text-align: left;
  margin-bottom: 16px;
}

.dist-row {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-bottom: 4px;
  font-size: 0.85rem;
}

.dist-label {
  width: 14px;
  text-align: right;
  flex-shrink: 0;
}

.dist-bar {
  background: var(--color-absent);
  padding: 2px 6px;
  min-width: 20px;
  text-align: right;
  border-radius: 2px;
  font-size: 0.8rem;
}

.dist-bar.highlight {
  background: var(--color-correct);
}

/* Game over modal */
.answer-reveal {
  font-size: 1.4rem;
  letter-spacing: 0.2em;
  margin: 12px 0;
  color: var(--color-correct);
}

.modal-btn {
  background: var(--color-correct);
  color: white;
  border: none;
  padding: 12px 24px;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  margin-top: 12px;
  font-family: inherit;
}

.modal-btn:active {
  opacity: 0.8;
}

/* Help modal */
.help-content {
  text-align: left;
  font-size: 0.85rem;
  line-height: 1.6;
}

.help-content h3 {
  margin-top: 12px;
  margin-bottom: 4px;
}

.example-tiles {
  display: flex;
  gap: 4px;
  margin: 8px 0;
}

.example-tile {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 700;
  border: 2px solid var(--color-tile-border);
}
