:root {
  /* Main application background color (light mode) */
  --app-bg: #f2f2f2;
  /* Final result review screen background (light mode) */
  --result-bg: #e5e5e5;
  /* Background color for table row hovers (light mode) */
  --table-hover-bg: #d6d6d6;
  /* Scrollbar track background color (light mode) */
  --scrollbar-track-bg: rgba(228, 228, 228, 0.25);
  /* Scrollbar thumb background color (light mode) */
  --scrollbar-thumb-bg: rgba(161, 161, 161, 0.25);
  /* Box shadow color for the active input focus (light mode) */
  --input-focus-shadow: gray;
  /* Border color for the active dropdown focus (light mode) */
  --input-focus-border: #ced4da;
  /* Shadow color for keyboard shortcuts kbd elements (light mode) */
  --kbd-shadow: rgba(0, 0, 0, 0.1);
  /* Hover background color for dark theme buttons (light mode) */
  --btn-dark-hover-bg: #343a40;
  /* Skipped answer text color in the review table (light mode) */
  --review-skipped-color: #dc3545;

  /* Valid input shadow and border (light mode) */
  --input-valid-shadow: rgba(25, 135, 84, 0.4);
  --input-valid-border: #198754;

  /* Invalid input shadow (light mode) */
  --input-invalid-shadow: rgba(253, 126, 20, 0.6);

  /* Toast notification colors (light mode) */
  --toast-bg: rgba(255, 255, 255, 0.9);
  --toast-border: rgba(0, 0, 0, 0.1);
  --toast-color: #212529;
  --toast-shadow: rgba(0, 0, 0, 0.15);

  /* Offline Status Badge colors (light mode) */
  --offline-badge-bg: rgba(253, 126, 20, 0.1);
  --offline-badge-border: rgba(253, 126, 20, 0.3);
  --offline-badge-color: #d9480f;
}

html {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
}

.preload,
.preload * {
  transition: none !important;
}

body.bg-dark {
  /* Main application background color (dark mode) */
  --app-bg: #212529;
  /* Final result review screen background (dark mode) */
  --result-bg: #181a1b;
  /* Background color for table row hovers (dark mode) */
  --table-hover-bg: #3a3a3a;
  /* Scrollbar track background color (dark mode) */
  --scrollbar-track-bg: rgba(228, 228, 228, 0.15);
  /* Scrollbar thumb background color (dark mode) */
  --scrollbar-thumb-bg: rgba(161, 161, 161, 0.15);
  /* Box shadow color for the active input focus (dark mode) */
  --input-focus-shadow: rgba(255, 255, 255, 0.2);
  /* Border color for the active dropdown focus (dark mode) */
  --input-focus-border: #495057;
  /* Shadow color for keyboard shortcuts kbd elements (dark mode) */
  --kbd-shadow: rgba(255, 255, 255, 0.1);
  /* Hover background color for light theme buttons (dark mode) */
  --btn-light-hover-bg: #e2e6ea;
  /* Hover border color for light theme buttons (dark mode) */
  --btn-light-hover-border: #dae0e5;
  /* Skipped answer text color in the review table (dark mode) */
  --review-skipped-color: #ff8787;

  /* Valid input shadow and border (dark mode) */
  --input-valid-shadow: rgba(46, 204, 113, 0.4);
  --input-valid-border: #2ecc71;

  /* Invalid input shadow (dark mode) */
  --input-invalid-shadow: rgba(255, 159, 67, 0.6);

  /* Toast notification colors (dark mode) */
  --toast-bg: rgba(33, 37, 41, 0.9);
  --toast-border: rgba(255, 255, 255, 0.1);
  --toast-color: #f8f9fa;
  --toast-shadow: rgba(0, 0, 0, 0.4);

  /* Offline Status Badge colors (dark mode) */
  --offline-badge-bg: rgba(255, 159, 67, 0.15);
  --offline-badge-border: rgba(255, 159, 67, 0.4);
  --offline-badge-color: #ff9f43;
}

body {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  width: 100%;
  height: 100dvh;

  /* Very light gray base instead of pure white for all non-dark modes */
  background-color: var(--app-bg);

  transition: background-color 0.5s ease;

  /*
   * Ensure a minimum gap from the top on all devices,
   * while respecting larger safe areas (like notches).
   */
  padding-top: max(30px, env(safe-area-inset-top));
  padding-bottom: env(safe-area-inset-bottom);

  overscroll-behavior: none;
}

#menu,
#game,
#result {
  display: flex;
  flex-direction: column;
  text-align: center;
  width: 100%;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

/* Center the main content vertically using auto-margins (for menu/result) */
.centering-wrapper {
  width: 100%;
  padding: 1rem 0;

  margin-top: auto;
  margin-bottom: auto;
}

/* Game-specific centering using transitionable flex-spacers */
.game-spacer {
  flex-grow: 1;
  transition: flex-grow 0.7s cubic-bezier(0.3, 1, 0.3, 1);
}

.game-content-wrapper {
  width: 100%;
  padding: 1rem 0;
}

/* On mobile/small screens, add back the optical shift */
@media (max-width: 768px), (max-height: 600px) {
  .centering-wrapper {
    padding-bottom: 6vh;
  }

  #game {
    overflow-y: hidden;
  }
}

/* On desktop, keep everything locked and perfectly centered */
@media (min-width: 769px) and (min-height: 601px) {
  #menu,
  #game,
  #result {
    overflow-y: hidden;
  }
}

#stats-row {
  width: 100%;
  margin: 0;
  padding: 0.5rem 1rem;
  background-color: var(--app-bg);
  transition: background-color 0.5s ease;
}

#result {
  align-items: center;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  /* Ensure it stays above game */
  z-index: 1050;
}

#menu,
#option-wrapper * {
  transition:
    background-color 0.5s ease,
    color 0.1s ease;
}

/*
 * Light-mode hover overrides.
 * Bootstrap's btn-outline-dark hover fills with near-black, which is
 * too harsh against the #f2f2f2 base.  Override to a slightly darker
 * gray so the hover is clearly visible but tonally consistent.
 * The :not(.bg-dark) guard keeps dark-mode buttons untouched.
 */
body:not(.bg-dark) .btn-dark:hover:not(:disabled):not(.active) {
  background-color: var(--btn-dark-hover-bg);
  border-color: var(--btn-dark-hover-bg);
}

/*
 * table-hover: customize the bootstrap hover background overlay
 * to ensure high visibility across both light and dark backgrounds.
 */
.table-hover > tbody > tr:hover > * {
  --bs-table-bg-state: var(--table-hover-bg);
}

/*
 * Dark-mode hover overrides.
 * Bootstrap's btn-outline-light hover and .active both fill near-white,
 * making a hovered button look identical to a selected one.
 * Override hover to a mid-gray so the two states are clearly distinct.
 * :not(.active) ensures the selected button keeps its bright fill.
 */
body.bg-dark .btn-light:hover:not(:disabled):not(.active) {
  background-color: var(--btn-light-hover-bg);
  border-color: var(--btn-light-hover-border);
}

#main-menu-group {
  width: 260px;
  max-width: 100%;
  transition: width 0.35s cubic-bezier(0.4, 0, 0.2, 1);
}

#main-menu-group.expanded {
  width: 400px;
}

#option-wrapper {
  width: 100%;
  overflow: hidden;
  opacity: 0;
  max-height: 0;
  margin-top: -2px;
  border-color: #00000000;
  transition:
    opacity,
    border,
    padding,
    max-height;
  transition-duration:
    0.3s,
    0.5s;
  transition-timing-function: ease;
}

#option-wrapper.collapsed {
  border-color: inherit;
  opacity: 1;
  max-height: 284px;
  height: 284px;
}

.form-select:focus {
  border-color: var(--input-focus-border) !important;
  box-shadow: none !important;
}

/*
 * Option Wrapper Border System
 * Uses variables to ensure consistent border colors across all groups.
 */
#option-wrapper {
  /* Group 1 */
  --opt-border-1: #6c757d;
  /* Group 2 */
  --opt-border-2: #adb5bd;
  /* Group 3 */
  --opt-border-3: #495057;
}

body.bg-dark #option-wrapper {
  /* Group 1 */
  --opt-border-1: #adb5bd;
  /* Group 2 */
  --opt-border-2: #6c757d;
  /* Group 3 */
  --opt-border-3: #dee2e6;
}

/* Apply group borders */
#option-wrapper .border-group-1,
#option-wrapper select.border-group-1,
#option-wrapper .btn.border-group-1 {
  border: 1px solid var(--opt-border-1) !important;
}

#option-wrapper .btn.border-group-2 {
  border: 1px solid var(--opt-border-2) !important;
}

#option-wrapper .btn.border-group-3 {
  border: 1px solid var(--opt-border-3) !important;
}

/* Ensure borders don't collapse into double borders by overlapping them by 1px */
#option-wrapper .btn-group > .btn + .btn {
  margin-left: -1px !important;
}

#option-wrapper .btn-group-vertical > .btn + .btn {
  margin-top: -1px !important;
}

#option-wrapper .btn:hover,
#option-wrapper .btn:focus,
#option-wrapper .btn.active {
  z-index: 2 !important;
}

/* Ensure button groups span full width and space children evenly */
#option-wrapper .btn-group {
  display: flex;
  width: 100%;
}

#option-wrapper .btn-group .btn {
  flex: 1;
  width: min-content;
}

/* Options scroll container for paginated settings */
#options-scroll-container {
  height: 230px;
  overflow: hidden;
  scroll-behavior: smooth;
  width: 100%;
}

.options-page {
  height: 230px;
  box-sizing: border-box;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

#game-dakuten,
#game-tsu,
#game-combo,
#game-smallvowel,
#game-vowellength {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.25em;
}

#game-dakuten:before {
  content: "\309B \309C";
  font-size: 1.5em;
  line-height: 1;
}

#game-tsu:before {
  content: "\3063 \30C3";
  font-size: 1.5em;
  line-height: 1;
}

#game-combo:before {
  content: "\3083 \30E3";
  font-size: 1.5em;
  line-height: 1;
}

#game-smallvowel:before {
  content: "\3041 \30A1";
  font-size: 1.5em;
  line-height: 1;
}

#game-vowellength:before {
  content: "\30FC";
  font-size: 1.5em;
  line-height: 1;
}

#option-help {
  height: 46px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  border-top: 1px dashed var(--opt-border-2);
  padding: 0.25rem 0.5rem;
  transition: color 0.1s ease;
}

/*
 * Force the inner span flex item to allow wrapping and scaling
 * on narrow viewports, preventing browser layout engines from
 * ignoring soft wrap boundaries.
 */
#option-help span {
  min-width: 0;
}

#question {
  font-size: clamp(60px, 10vw, 100px);
}

#answer,
#answer:focus {
  background: none;
  border: none;
  outline: none;
  box-shadow: 0 0 5px var(--input-focus-shadow);
  text-align: center;
  width: 100%;
}

#result {
  /* Solid background that is slightly darker than the base background to keep the main screen fully hidden */
  background-color: var(--result-bg) !important;
  top: 100%;
  height: 100%;
}

#review-wrapper {
  margin-bottom: 1em;
  max-height: 50vh;
}

#review-wrapper::-webkit-scrollbar {
  width: 10px;
}

#review-wrapper::-webkit-scrollbar-track {
  background-color: var(--scrollbar-track-bg);
  border-radius: 100px;
}

#review-wrapper::-webkit-scrollbar-thumb {
  background-color: var(--scrollbar-thumb-bg);
  border-radius: 100px;
}

#review-table a {
  font-size: larger;
  color: inherit;
}

#review-table td {
  font-family:
    "Noto Sans JP", "Klee One", "Noto Serif JP", "Yuji Syuku", sans-serif,
    system-ui, serif;
}

/* Color for skipped questions in the review table */
#review-table .text-danger {
  color: var(--review-skipped-color) !important;
}

th,
td {
  padding: 0.5em;
}

/* Slide animations: replacing jQuery slideUp/slideDown/animate */
#menu {
  transition:
    transform 0.5s ease,
    opacity 0.5s ease;
}

#menu.slide-up,
#game.slide-up {
  transform: translateY(-100%);
  opacity: 0;
  pointer-events: none;
  display: none;
}

#result {
  transition: top 0.8s ease;
}

#result.slide-in {
  top: 0;
}

/*
 * Bootstrap sets pointer-events:none on .btn:disabled, which prevents the
 * cursor property from being evaluated by the browser.
 * Re-enable pointer events so cursor:not-allowed is actually visible to the user,
 * while the native `disabled` attribute already blocks all click interactions.
 */
#review:disabled,
#start:disabled {
  pointer-events: auto !important;
  cursor: not-allowed !important;
}

.btn {
  transition:
    background-color 0.3s ease,
    color 0.1s ease,
    border-color 0.3s ease,
    box-shadow 0.3s ease !important;
}

/*
 * Compact layout padding and margins for mobile and short viewports.
 * We use transitions here to ensure that when the keyboard opens/closes,
 * or when the viewport resizes, the elements "slide" into their new positions.
 */
#game #stats-row,
#game #question-wrapper,
#game #answer-wrapper,
#game #hints-wrapper {
  transition:
    padding 0.7s cubic-bezier(0.3, 1, 0.3, 1),
    margin 0.7s cubic-bezier(0.3, 1, 0.3, 1),
    gap 0.7s cubic-bezier(0.3, 1, 0.3, 1);
}

@media (max-height: 600px), (max-width: 768px) {
  body.keyboard-open .centering-wrapper {
    margin-top: 1vh !important;
    margin-bottom: 0.5rem !important;
  }

  #game #stats-row {
    margin-bottom: 0.5rem !important;
  }

  #game #question-wrapper {
    padding-top: 1.5rem !important;
    padding-bottom: 1.5rem !important;
  }

  #game #answer-wrapper {
    margin-bottom: 1.5rem !important;
  }

  #game #hints-wrapper {
    gap: 0.75rem !important;
  }

  body.keyboard-open #game #stats-row {
    margin-bottom: 0 !important;
  }

  body.keyboard-open #game #question-wrapper {
    padding-top: 0.75rem !important;
    padding-bottom: 0.75rem !important;
  }

  body.keyboard-open #game #answer-wrapper {
    margin-bottom: 1rem !important;
  }

  body.keyboard-open #game #hints-wrapper {
    gap: 0.5rem !important;
  }
}

/* Font size reductions only when the display is *really* small (width-based).
   This ensures the font size is consistent whether the keyboard is visible or not. */
@media (max-width: 400px) {
  #game .stat-item {
    font-size: 1rem;
  }

  #question {
    font-size: clamp(20px, 15vh, 60px);
  }

  #game #hints-wrapper {
    font-size: 0.75rem;
    gap: 0.2rem !important;
  }
}

/* Allow hints to wrap on very small screens */
@media (max-width: 330px) {
  #question {
    font-size: clamp(12px, 15vw, 40px);
  }

  #game #hints-wrapper {
    display: flex;
    flex-direction: column;
    /* Required to allow items to move to new lines */
    flex-wrap: wrap;
  }

  #game #stats-row > * {
    flex-basis: 100%;
    flex-shrink: 0;
    flex-grow: 0;
  }

  #game #stop {
    order: 1;
  }
}

/*
 * Keyboard keys (kbd) styling.
 * Inverted themes are applied via JS (bg-white/bg-dark).
 * We add transitions and a subtle shadow for a premium feel.
 */
kbd {
  transition:
    background-color 0.5s ease,
    color 0.1s ease,
    box-shadow 0.5s ease;
  padding: 0.2rem 0.4rem;
  border-radius: 0.25rem;
  box-shadow: 0 2px 0 var(--kbd-shadow);
}

/*
 * Hide the "Press Tab to end" hint on touch-only devices (phones/tablets).
 * pointer: coarse = no fine pointer (no mouse), hover: none = no hover
 * capability — together these reliably exclude devices with a physical or
 * USB/Bluetooth keyboard while keeping the hint visible on desktop.
 */
@media (pointer: coarse) and (hover: none) {
  .hint-keyboard {
    display: none;
  }
}

/*
 * Toast Notification styling
 * Displays floating messages to help users with input tips.
 */
#toast-container {
  pointer-events: none;
  margin-top: max(10px, env(safe-area-inset-top));
  z-index: 1100;
  width: max-content;
  max-width: calc(100vw - 32px);
}

.custom-toast {
  background: var(--toast-bg);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  border: 1px solid var(--toast-border);
  color: var(--toast-color);
  padding: 10px 20px;
  border-radius: 50px;
  box-shadow: 0 4px 15px var(--toast-shadow);
  font-size: 0.9rem;
  display: flex;
  align-items: flex-start;
  gap: 10px;
  opacity: 0;
  transform: translateY(-20px);
  transition:
    opacity 0.3s cubic-bezier(0.4, 0, 0.2, 1),
    transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  pointer-events: auto;
  margin-bottom: 10px;
  white-space: nowrap;
}

.custom-toast i {
  margin-top: 2px;
  flex-shrink: 0;
}

.custom-toast.show {
  opacity: 1;
  transform: translateY(0);
}

/* Responsive adjustments for narrow viewports */
@media (max-width: 575.98px) {
  .custom-toast {
    white-space: normal;
    border-radius: 20px;
    text-align: left;
  }
}

#answer.is-valid {
  border-color: var(--input-valid-border) !important;
  box-shadow: 0 0 10px var(--input-valid-shadow) !important;
  animation: pop-success 0.25s ease-out;
}

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

#answer.is-invalid {
  box-shadow: 0 0 10px var(--input-invalid-shadow) !important;
  animation: shake 0.4s ease-in-out;
}

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

/*
 * Offline Badge styling
 * Displays a persistent, non-intrusive badge in the top-right corner when connection is lost.
 */
#offline-badge {
  position: fixed;
  top: max(15px, env(safe-area-inset-top));
  right: max(15px, env(safe-area-inset-right));
  background: var(--offline-badge-bg);
  border: 1px solid var(--offline-badge-border);
  color: var(--offline-badge-color);
  padding: 6px 14px;
  border-radius: 50px;
  box-shadow: 0 4px 10px var(--toast-shadow);
  font-size: 0.85rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 6px;
  opacity: 0;
  transform: translateY(-10px);
  transition:
    opacity 0.3s cubic-bezier(0.4, 0, 0.2, 1),
    transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  pointer-events: none;
  z-index: 1050;
}

#offline-badge.show {
  opacity: 1;
  transform: translateY(0);
}
