:root {
      --bg: #302e2b;
      --bg-deep: #262421;
      --panel: #3a3835;
      --panel-border: #4a4744;
      --moss: #6f8a55;
      --cream: #ece5cb;
      --accent: #7fa650;
      --accent-glow: rgba(127, 166, 80, 0.55);
      --danger: #d9534f;
      --danger-glow: rgba(217, 83, 79, 0.55);
      --promote: #d9a441;
      --promote-glow: rgba(217, 164, 65, 0.55);
      --text: #e9e8e6;
      --text-dim: #a3a09b;
      --white-piece: #ffffff;
      --black-piece: #111111;
    }

    * {
      box-sizing: border-box;
      -webkit-tap-highlight-color: transparent;
    }

    html,
    body {
      margin: 0;
      padding: 0;
      height: 100%;
      background:
        radial-gradient(ellipse at 50% -10%, rgba(127, 166, 80, 0.06), transparent 55%),
        var(--bg);
      color: var(--text);
      font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
      overflow-x: hidden;
      overscroll-behavior: none;
      touch-action: manipulation;
    }

    /* ---------- Screen management ---------- */
    .screen {
      display: none;
      min-height: 100dvh;
      width: 100%;
      box-sizing: border-box;
    }

    .screen.active {
      display: flex;
      flex-direction: column;
      align-items: center;
    }

    #home-screen.active {
      justify-content: center;
      padding: 40px 20px;
    }

    #rules-screen.active {
      justify-content: flex-start;
      padding: 30px 20px;
    }

    #game-screen.active {
      justify-content: center;
      align-items: center;
      padding: 64px 16px 24px;
      position: relative;
      touch-action: manipulation;
    }

    /* ---------- Home screen ---------- */
    .home-card {
      max-width: 380px;
      width: 100%;
      text-align: center;
    }

    h1.game-title {
      margin: 0;
      font-size: clamp(28px, 7vw, 42px);
      letter-spacing: -0.01em;
      font-weight: 800;
      color: var(--text);
    }

    .subtitle {
      margin: 4px 0 0;
      font-size: 12px;
      letter-spacing: 0.14em;
      text-transform: uppercase;
      color: var(--text-dim);
      font-weight: 500;
    }

    .name-fields {
      display: flex;
      flex-direction: column;
      gap: 14px;
      margin: 28px 0 22px;
      text-align: left;
    }

    .name-field {
      display: flex;
      flex-direction: column;
      gap: 6px;
    }

    .name-field-label {
      display: flex;
      align-items: center;
      gap: 7px;
      font-size: 11px;
      font-weight: 700;
      letter-spacing: 0.08em;
      text-transform: uppercase;
      color: var(--text-dim);
    }

    .mini-dot {
      width: 10px;
      height: 10px;
      border-radius: 50%;
      flex: none;
    }

    .mini-dot.white {
      background: var(--white-piece);
      border: 1.5px solid #1a1a1a;
    }

    .mini-dot.black {
      background: var(--black-piece);
      border: 1.5px solid #fff;
    }

    .name-field input[type="text"] {
      background: var(--bg-deep);
      border: 1px solid var(--panel-border);
      color: var(--text);
      font-size: 15px;
      padding: 11px 13px;
      border-radius: 8px;
      font-family: inherit;
      width: 100%;
    }

    .name-field input[type="text"]::placeholder {
      color: #6e6b66;
    }

    .name-field input[type="text"]:focus {
      outline: none;
      border-color: var(--accent);
    }

    .menu-buttons {
      display: flex;
      flex-direction: column;
      gap: 10px;
    }

    .menu-btn {
      appearance: none;
      border: none;
      font-size: 14.5px;
      font-weight: 700;
      letter-spacing: 0.02em;
      padding: 13px 20px;
      border-radius: 10px;
      cursor: pointer;
      font-family: inherit;
      transition: filter 0.15s ease, transform 0.1s ease, opacity 0.15s ease;
    }

    .menu-btn.primary {
      background: var(--accent);
      color: #1c2214;
    }

    .menu-btn.secondary {
      background: var(--promote);
      color: #241a05;
    }

    .menu-btn.ghost {
      background: transparent;
      border: 1px solid var(--panel-border);
      color: var(--text);
    }

    .menu-btn:hover {
      filter: brightness(1.08);
    }

    .menu-btn:active {
      transform: scale(0.97);
    }

    .menu-btn:disabled {
      opacity: 0.4;
      cursor: not-allowed;
      filter: none;
      transform: none;
    }

    /* ---------- Game screen header controls ---------- */
    .icon-btn {
      position: absolute;
      top: 16px;
      width: 38px;
      height: 38px;
      border-radius: 50%;
      border: 1px solid var(--panel-border);
      background: var(--panel);
      color: var(--text-dim);
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      z-index: 10;
      transition: filter 0.15s ease, transform 0.1s ease, color 0.15s ease;
    }

    .icon-btn.home-pos {
      left: 16px;
    }

    .icon-btn.settings-pos {
      right: 16px;
    }

    .icon-btn.forfeit-pos {
      right: 60px;
    }

    .icon-btn.forfeit-pos,
    #forfeit-btn {
      color: var(--danger);
      border-color: var(--danger);
    }
    .icon-btn.forfeit-pos:hover,
    #forfeit-btn:hover {
      background: var(--danger);
      color: #fff;
    }

    .icon-btn:hover {
      filter: brightness(1.15);
      color: var(--text);
    }

    .icon-btn:active {
      transform: scale(0.94);
    }

    .icon-btn svg {
      width: 18px;
      height: 18px;
    }

    .ctrl-btn {
      appearance: none;
      border: 1px solid var(--panel-border);
      background: var(--panel);
      color: var(--text);
      font-size: 12.5px;
      font-weight: 600;
      padding: 8px 14px;
      border-radius: 8px;
      cursor: pointer;
      font-family: inherit;
      transition: filter 0.15s ease, opacity 0.15s ease;
    }

    .ctrl-btn:disabled {
      opacity: 0.35;
      cursor: not-allowed;
    }

    .ctrl-btn:hover:not(:disabled) {
      filter: brightness(1.15);
    }

    .table-layout {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      gap: 12px;
      width: 100%;
      max-width: 640px;
    }

    .turn-banner {
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 10px;
      padding: 10px 22px;
      border-radius: 10px;
      background: var(--panel);
      border: 1px solid var(--panel-border);
      box-shadow: 0 2px 10px rgba(0, 0, 0, 0.25);
      min-width: 220px;
    }

    .turn-banner .indicator {
      width: 14px;
      height: 14px;
      border-radius: 50%;
      transition: background 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease;
      flex: none;
    }

    .turn-banner .indicator.white {
      background: var(--white-piece);
      border: 1.5px solid #1a1a1a;
      box-shadow: 0 0 8px rgba(255, 255, 255, 0.35);
    }

    .turn-banner .indicator.black {
      background: var(--black-piece);
      border: 1.5px solid #ffffff;
      box-shadow: 0 0 8px rgba(0, 0, 0, 0.5);
    }

    .turn-banner .label {
      font-size: 14px;
      font-weight: 600;
      letter-spacing: 0.02em;
      color: var(--text);
    }

    .timer-display {
      font-family: 'SF Mono', SFMono-Regular, Consolas, 'Liberation Mono', Menlo, monospace;
      font-size: 13px;
      font-weight: 800;
      letter-spacing: 0.03em;
      color: var(--promote);
      margin-left: 2px;
      padding-left: 10px;
      border-left: 1px solid var(--panel-border);
      transition: color 0.2s ease;
    }

    .timer-display.low-time {
      color: var(--danger);
      animation: timer-pulse 0.75s ease-in-out infinite;
    }

    @keyframes timer-pulse {

      0%,
      100% {
        opacity: 1;
      }

      50% {
        opacity: 0.4;
      }
    }

    .status-line {
      min-height: 18px;
      margin-top: 6px;
      margin-bottom: 0;
      font-size: 12px;
      font-weight: 600;
      letter-spacing: 0.02em;
      text-align: center;
      color: var(--promote);
      transition: color 0.2s ease;
      max-width: 340px;
    }

    .status-line.empty {
      visibility: hidden;
    }

    .hud-controls {
      display: flex;
      gap: 8px;
      margin-top: 6px;
    }

    .hud-mini-btn {
      appearance: none;
      border: 1px solid var(--panel-border);
      background: var(--panel);
      color: var(--text-dim);
      font-size: 11px;
      font-weight: 600;
      padding: 5px 12px;
      border-radius: 999px;
      cursor: pointer;
      font-family: inherit;
      transition: filter 0.15s ease, opacity 0.15s ease;
    }

    .hud-mini-btn:disabled {
      opacity: 0.3;
      cursor: not-allowed;
    }

    .hud-mini-btn:hover:not(:disabled) {
      filter: brightness(1.15);
    }

    .desktop-hud {
      display: none;
      flex-direction: column;
      align-items: center;
      gap: 10px;
    }

    .desktop-controls-row {
      display: flex;
      align-items: center;
      gap: 8px;
      flex-wrap: wrap;
      justify-content: center;
    }

    .hud-banner {
      display: none;
    }

    /* Default CSS Rules for Mobile/Touch view */
    body.mobile-layout .hud-banner {
      display: flex;
      flex-direction: column;
      align-items: center;
      width: 100%;
    }

    body.mobile-layout .hud-banner.hud-top {
      transform: rotate(180deg);
    }

    body.mobile-layout .desktop-hud {
      display: none !important;
    }

    /* Default CSS Rules for Desktop view */
    body.desktop-layout .hud-banner {
      display: none !important;
    }

    body.desktop-layout .desktop-hud {
      display: flex;
    }

    .board-frame {
      position: relative;
      width: min(88vw, 540px);
      aspect-ratio: 1/1;
      padding: 8px;
      border-radius: 6px;
      background: var(--bg-deep);
      box-shadow:
        0 24px 60px rgba(0, 0, 0, 0.5),
        0 4px 16px rgba(0, 0, 0, 0.4),
        inset 0 0 0 1px rgba(255, 255, 255, 0.03);
      flex: none;
      transition: box-shadow 0.2s ease;
    }

    #board-canvas {
      display: block;
      width: 100%;
      height: 100%;
      border-radius: 2px;
      cursor: pointer;
      touch-action: manipulation;
    }

    /* ---------- Overlays & Modals ---------- */
    .overlay {
      position: fixed;
      inset: 0;
      display: flex;
      align-items: center;
      justify-content: center;
      background: rgba(18, 17, 15, 0.85);
      opacity: 0;
      pointer-events: none;
      transition: opacity 0.15s ease-out;
      will-change: opacity;
      z-index: 50;
      padding: 20px;
    }

    .overlay.visible {
      opacity: 1;
      pointer-events: auto;
    }

    .overlay-card {
      background: var(--panel);
      border: 1px solid var(--panel-border);
      border-radius: 14px;
      padding: 28px 32px;
      text-align: center;
      box-shadow: 0 30px 80px rgba(0, 0, 0, 0.55);
      max-width: 380px;
      width: 100%;
      transform: translateY(4px) scale(0.99);
      transition: transform 0.15s ease-out;
      will-change: transform;
    }

    .overlay.visible .overlay-card {
      transform: translateY(0) scale(1);
    }

    .overlay-result {
      font-size: 24px;
      font-weight: 700;
      margin: 0 0 8px;
    }

    .overlay-reason {
      margin: 0 0 22px;
      font-size: 13px;
      color: var(--text-dim);
      line-height: 1.5;
    }

    .overlay-actions {
      display: flex;
      gap: 10px;
      justify-content: center;
      margin-top: 18px;
    }

    .overlay-btn {
      appearance: none;
      border: none;
      background: var(--accent);
      color: #1c2214;
      font-size: 14px;
      font-weight: 700;
      padding: 11px 22px;
      border-radius: 8px;
      cursor: pointer;
      font-family: inherit;
    }

    .overlay-btn.ghost {
      background: transparent;
      border: 1px solid var(--panel-border);
      color: var(--text);
    }

    /* Mode selection & Settings modals */
    .modal-title {
      font-size: 18px;
      font-weight: 700;
      margin: 0 0 16px;
    }

    .mode-options {
      display: flex;
      flex-direction: column;
      gap: 12px;
      margin-bottom: 20px;
      text-align: left;
    }

    .mode-opt {
      background: var(--bg-deep);
      border: 1px solid var(--panel-border);
      border-radius: 8px;
      padding: 12px;
      cursor: pointer;
      transition: border-color 0.15s ease, background-color 0.15s ease, box-shadow 0.15s ease;
    }

    .mode-opt:hover {
      border-color: var(--accent);
    }

    .mode-opt-title {
      font-size: 14px;
      font-weight: 700;
      color: var(--text);
      margin-bottom: 4px;
      transition: color 0.15s ease;
    }

    .mode-opt-desc {
      font-size: 11.5px;
      color: var(--text-dim);
      line-height: 1.4;
      transition: color 0.15s ease;
    }

    .mode-opt.selected {
      border-color: #10b981;
      background: #ecfdf5;
      box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.25);
    }

    .mode-opt.selected .mode-opt-title {
      color: #065f46;
    }

    .mode-opt.selected .mode-opt-desc {
      color: #047857;
    }

    .mode-opt.selected .sub-btn:not(.active) {
      background: #ffffff;
      border-color: #a7f3d0;
      color: #065f46;
    }

    .timer-subs {
      display: flex;
      gap: 6px;
      margin-top: 8px;
    }

    .sub-btn {
      flex: 1;
      background: var(--panel);
      border: 1px solid var(--panel-border);
      color: var(--text);
      font-size: 11px;
      font-weight: 600;
      padding: 6px;
      border-radius: 6px;
      cursor: pointer;
    }

    .sub-btn.active {
      background: var(--accent);
      color: #1c2214;
      border-color: var(--accent);
    }

    .setting-row {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 12px 0;
      border-bottom: 1px solid var(--panel-border);
      text-align: left;
    }

    .setting-row:last-child {
      border-bottom: 1px solid var(--panel-border);
      margin-bottom: 4px;
    }

    .setting-info {
      display: flex;
      flex-direction: column;
      gap: 2px;
    }

    .setting-label {
      font-size: 14px;
      font-weight: 700;
      color: var(--text);
    }

    .setting-desc {
      font-size: 11.5px;
      color: var(--text-dim);
    }

    .toggle-switch {
      position: relative;
      width: 42px;
      height: 22px;
      flex: none;
    }

    .toggle-switch input {
      opacity: 0;
      width: 0;
      height: 0;
    }

    .toggle-slider {
      position: absolute;
      inset: 0;
      background-color: var(--bg-deep);
      border: 1px solid var(--panel-border);
      border-radius: 999px;
      cursor: pointer;
      transition: background-color 0.2s ease, border-color 0.2s ease;
    }

    .toggle-slider::before {
      content: "";
      position: absolute;
      top: 2px;
      left: 2px;
      width: 16px;
      height: 16px;
      border-radius: 50%;
      background: var(--text-dim);
      transition: transform 0.2s ease, background-color 0.2s ease;
    }

    .toggle-switch input:checked+.toggle-slider {
      background-color: rgba(127, 166, 80, 0.35);
      border-color: var(--accent);
    }

    .toggle-switch input:checked+.toggle-slider::before {
      transform: translateX(20px);
      background: var(--accent);
    }

    /* Rules Screen layout */
    .rules-container {
      max-width: 600px;
      width: 100%;
      background: var(--panel);
      border: 1px solid var(--panel-border);
      border-radius: 12px;
      padding: 24px;
      margin-top: 20px;
    }

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

    .rules-header h2 {
      margin: 0;
      font-size: 20px;
    }

    .tier-box {
      background: var(--bg-deep);
      border-radius: 8px;
      padding: 16px;
      margin-bottom: 16px;
    }

    .tier-box h3 {
      margin: 0 0 10px;
      font-size: 14px;
      color: var(--accent);
      text-transform: uppercase;
      letter-spacing: 0.05em;
    }

    .tier-box ul {
      margin: 0;
      padding-left: 18px;
      color: var(--text-dim);
      font-size: 13.5px;
      line-height: 1.6;
    }

    .rulebook-link {
      display: block;
      width: 100%;
      text-align: center;
      text-decoration: none;
      box-sizing: border-box;
    }