:root {
  --bg: #1a1a1a;
  --panel: #252525;
  --panel-2: #1f1f1f;
  --border: #3a3a3a;
  --text: #f5f5f5;
  --text-sub: #a0a0a0;
  --accent: #cc785c;
  --accent-2: #a35d44;
  --eye: #1a1a1a;
  --ok: #6fbf73;
  --ng: #d96c6c;

  --cursor: var(--accent);
  --typing-dim: rgba(245, 245, 245, 0.55);
  --bubble-error: var(--ng);

  --system-border: var(--text-sub);
  --system-bg: var(--panel-2);
  --cooldown: var(--accent);
  --toggle-on-bg: rgba(204, 120, 92, 0.12);

  --font-ui: "Inter", -apple-system, "Hiragino Kaku Gothic ProN", "Noto Sans JP",
    system-ui, sans-serif;
  --font-mono: "DM Mono", "SF Mono", Consolas, "Courier New", monospace;

  /* M4: ブロック崩し */
  --game-bg: #141414;
  --game-paddle: var(--accent);
  --game-paddle-2: var(--accent-2);
  --game-ball: var(--text);
  --game-ball-glow: rgba(245, 245, 245, 0.35);
  --game-line: #2a2a2a;
  --game-hp: var(--accent);
  --game-hp-empty: #4a2a22;
  --brick-row1: #d98a6c;
  --brick-row2: #cc785c;
  --brick-row3: #b86a50;
  --brick-row4: #a35d44;
  --brick-row5: #804938;
  --overlay-bg: rgba(20, 20, 20, 0.78);
  --overlay-border: var(--accent);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

html {
  background: var(--bg);
  color: var(--text);
}

body {
  max-width: 720px;
  margin: 0 auto;
  padding: 0 16px;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
}

button {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
  cursor: pointer;
}

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

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* header */
.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 16px;
  margin: 0 -16px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.app-title {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  line-height: 1.3;
  display: flex;
  align-items: center;
  gap: 10px;
}

.app-title-mark {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--accent);
  padding: 2px 8px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  letter-spacing: 0.02em;
}

.app-title-text {
  color: var(--text);
}

.header-toggles {
  display: flex;
  gap: 8px;
}

.toggle-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 32px;
  padding: 0 10px;
  background: transparent;
  color: var(--text-sub);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  transition:
    color 120ms ease,
    border-color 120ms ease,
    background 120ms ease;
}

.toggle-btn:hover:not(:disabled) {
  color: var(--text);
  border-color: var(--accent);
}

.toggle-btn[aria-pressed="true"] {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--toggle-on-bg);
}

.toggle-btn:active:not(:disabled) {
  transform: translateY(1px);
}

.toggle-icon {
  font-size: 14px;
}

.rl-counter {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text-sub);
  padding: 0 6px;
  align-self: center;
  letter-spacing: 0.02em;
  font-variant-numeric: tabular-nums;
}

.rl-counter[data-state="warn"] {
  color: var(--accent);
}

.rl-counter[data-state="zero"] {
  color: var(--ng);
}

/* main */
.app-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 0;
}

/* stage */
#stage {
  position: relative;
  width: 240px;
  height: 240px;
  margin: 32px auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

#stage .clawd {
  width: 80%;
  height: 80%;
  animation: clawd-bounce 2.4s ease-in-out infinite;
  transform-origin: 50% 100%;
}

#stage g[data-expr] {
  opacity: 0;
  transition: opacity 200ms ease;
}

#stage g[data-expr].is-active {
  opacity: 1;
}

@keyframes clawd-bounce {
  0%,
  100% {
    transform: translateY(0);
  }
  45% {
    transform: translateY(-6px);
  }
  50% {
    transform: translateY(-6px);
  }
  55% {
    transform: translateY(0);
  }
}

/* chat log */
.chat-log {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 240px;
  max-height: 50vh;
  overflow-y: auto;
  padding: 4px 2px;
}

.chat-msg {
  padding: 10px 14px;
  border-radius: 12px;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.chat-msg--clawd {
  background: var(--bg);
  border-left: 2px solid var(--accent);
  border-radius: 0 12px 12px 0;
  padding-left: 12px;
}

.chat-msg--you {
  background: var(--panel);
  align-self: flex-end;
  max-width: 80%;
}

.chat-name {
  display: block;
  font-size: 12px;
  font-weight: 600;
  margin-bottom: 2px;
  color: var(--accent);
}

.chat-msg--you .chat-name {
  color: var(--text-sub);
}

.chat-body {
  margin: 0;
  font-size: 15px;
  line-height: 1.6;
  white-space: pre-wrap;
}

/* input row */
.input-row {
  position: sticky;
  bottom: 0;
  z-index: 10;
  background: var(--bg);
  border-top: 1px solid var(--border);
  padding: 12px 0;
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
  margin: 0 -16px;
}

.chat-form {
  display: flex;
  gap: 8px;
  padding: 0 16px;
}

.chat-input {
  flex: 1;
  height: 40px;
  padding: 0 12px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: inherit;
  font-size: 16px;
  line-height: 1.4;
  transition: border-color 120ms ease;
}

.chat-input::placeholder {
  color: var(--text-sub);
}

.chat-input:focus {
  border-color: var(--accent);
  outline: none;
  box-shadow: 0 0 0 2px rgba(204, 120, 92, 0.25);
}

.chat-input:disabled,
.chat-input[readonly] {
  color: var(--typing-dim);
  opacity: 0.6;
  cursor: not-allowed;
}

.send-btn {
  height: 40px;
  min-width: 96px;
  padding: 0 18px;
  background: var(--accent);
  color: #1a1a1a;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition:
    background 120ms ease,
    transform 80ms ease;
}

.send-btn:hover:not(:disabled) {
  background: var(--accent-2);
}

.send-btn:active:not(:disabled) {
  transform: translateY(1px);
}

.send-btn[data-loading="true"]::before {
  content: "";
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* streaming / typing */
.typing-cursor {
  display: inline-block;
  width: 0.5ch;
  margin-left: 2px;
  color: var(--cursor);
  animation: cursor-blink 1s steps(2, end) infinite;
}

@keyframes cursor-blink {
  0%,
  50% {
    opacity: 1;
  }
  50.01%,
  100% {
    opacity: 0;
  }
}

.chat-msg--clawd.is-error {
  border-left-color: var(--bubble-error);
}

/* M3: system バブル（コマンド応答） */
.chat-msg--system {
  background: var(--system-bg);
  border-left: 2px solid var(--system-border);
  border-radius: 0 12px 12px 0;
  padding-left: 12px;
  animation: system-fadein 200ms ease-out;
}

.chat-msg--system .chat-name {
  color: var(--text-sub);
}

.chat-msg--system .cmd-list {
  margin: 6px 0 0;
  padding: 0;
}

.chat-msg--system .cmd-list dt {
  font-family: var(--font-mono);
  color: var(--accent);
  margin-top: 4px;
}

.chat-msg--system .cmd-list dd {
  margin: 0 0 4px 1.5em;
  color: var(--text);
}

.chat-msg--system a {
  color: var(--accent);
}

@keyframes system-fadein {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* M3: 429 cooldown variant */
.chat-msg--clawd.is-cooldown {
  border-left-color: var(--cooldown);
}

.chat-msg--clawd.is-cooldown .reset-time {
  display: inline-block;
  margin-left: 4px;
  color: var(--text-sub);
  font-family: var(--font-mono);
  font-size: 12px;
}

/* mobile */
@media (max-width: 480px) {
  body {
    padding: 0 12px;
  }

  .app-header {
    margin: 0 -12px;
    padding: 0 12px;
  }

  .app-title-text {
    font-size: 15px;
  }

  .toggle-btn .toggle-label {
    display: none;
  }

  .toggle-btn {
    padding: 0 8px;
  }

  .rl-counter {
    display: none;
  }

  #stage {
    width: min(70vw, 200px);
    height: min(70vw, 200px);
    margin: 20px auto;
  }

  .game-hud {
    font-size: 12px;
  }

  .game-overlay-title {
    font-size: 22px;
  }

  .game-overlay--intro .game-overlay-title {
    font-size: 14px;
  }

  .game-overlay-sub {
    font-size: 12px;
  }

  .chat-log {
    max-height: 40vh;
  }

  .input-row {
    margin: 0 -12px;
  }

  .chat-form {
    padding: 0 12px;
  }
}

/* M4: ブロック崩しモード */
body[data-mode="play"] #stage {
  background: var(--game-bg);
  border-color: var(--accent);
  /* チャットモードの 240px 固定から、画面に応じて拡大する */
  width: min(80vmin, 560px);
  height: min(80vmin, 560px);
  margin: 24px auto;
}

body[data-mode="play"] #chat-log {
  visibility: hidden;
  height: 0;
  min-height: 0;
  padding: 0;
  margin: 0;
  overflow: hidden;
}

#game-canvas {
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  cursor: none;
  background: var(--game-bg);
}

.game-hud {
  position: absolute;
  top: 6px;
  left: 8px;
  right: 8px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-family: var(--font-mono);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  pointer-events: none;
  z-index: 2;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.7);
}

.game-hud .score {
  letter-spacing: 0.04em;
}

.game-hud .hp {
  display: inline-flex;
  gap: 2px;
  font-size: 14px;
}

.game-hud .hp .hp-icon {
  color: var(--game-hp);
  animation: hp-shake 200ms ease;
}

.game-hud .hp .hp-icon--empty {
  color: var(--game-hp-empty);
}

@keyframes hp-shake {
  0%,
  100% {
    transform: translateX(0);
  }
  25% {
    transform: translateX(-2px);
  }
  75% {
    transform: translateX(2px);
  }
}

.game-help {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 6px;
  margin: 0;
  text-align: center;
  font-family: var(--font-ui);
  font-size: 12px;
  color: var(--text-sub);
  pointer-events: none;
  z-index: 2;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.7);
}

.game-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: var(--overlay-bg);
  backdrop-filter: blur(2px);
  text-align: center;
  padding: 16px;
  z-index: 3;
  animation: overlay-fadein 200ms ease-out;
}

.game-overlay-title {
  margin: 0;
  font-family: var(--font-ui);
  font-size: 28px;
  font-weight: 600;
  color: var(--accent);
  letter-spacing: 0.04em;
}

.game-overlay--intro .game-overlay-title {
  color: var(--text);
  font-size: 18px;
}

.game-overlay--gameover .game-overlay-title {
  color: var(--text-sub);
}

.game-overlay-sub {
  margin: 0;
  font-family: var(--font-ui);
  font-size: 13px;
  color: var(--text);
  white-space: pre-line;
  line-height: 1.5;
}

.game-overlay-actions {
  display: flex;
  gap: 8px;
  margin-top: 4px;
}

.game-btn {
  min-height: 36px;
  padding: 0 14px;
  font-family: var(--font-ui);
  font-weight: 600;
  font-size: 13px;
  border-radius: 8px;
  border: 1px solid transparent;
  cursor: pointer;
  transition:
    background 120ms ease,
    border-color 120ms ease;
}

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

.game-btn--primary:hover {
  background: var(--accent-2);
}

.game-btn--ghost {
  background: transparent;
  color: var(--text-sub);
  border-color: var(--border);
}

.game-btn--ghost:hover {
  color: var(--text);
  border-color: var(--accent);
}

@keyframes overlay-fadein {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

#exit-play-btn .toggle-icon {
  color: var(--ng);
}

/* reduced motion */
@media (prefers-reduced-motion: reduce) {
  #stage .clawd {
    animation: none;
  }
  #stage g[data-expr] {
    transition: none;
  }
  button,
  a,
  .toggle-btn,
  .send-btn,
  .chat-input {
    transition: none;
  }
  .typing-cursor {
    animation: none;
    opacity: 1;
  }
  .send-btn[data-loading="true"]::before {
    animation: none;
  }
  .chat-msg--system {
    animation: none;
  }
  .game-overlay {
    animation: none;
  }
  .game-hud .hp .hp-icon {
    animation: none;
  }
}
