:root {
  --bg: #0b0d10;
  --bg-elevated: #141820;
  --bg-panel: #1a1f2a;
  --border: #2a3344;
  --text: #f4f6fa;
  --text-muted: #9aa3b5;
  --accent: #4cc9f0;
  --accent-dim: #1f6f8b;
  --success: #3ddc97;
  --danger: #ff6b6b;
  --warning: #ffd166;
  --btn: #232a38;
  --btn-hover: #2e384c;
  --btn-active: #3d4a63;
  --focus: #7dd3fc;
  --radius: 14px;
  --shadow: 0 4px 20px rgba(0, 0, 0, 0.45);
  --tap-min: 56px;
  --font: system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
}

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

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

body {
  line-height: 1.35;
}

button,
textarea {
  font: inherit;
  color: inherit;
}

.app {
  max-width: 960px;
  margin: 0 auto;
  padding: 12px 14px 32px;
}

.header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
}

.brand h1 {
  margin: 0;
  font-size: clamp(1.5rem, 5vw, 2rem);
  font-weight: 800;
  letter-spacing: -0.02em;
}

.brand .subtitle {
  margin: 4px 0 0;
  color: var(--text-muted);
  font-size: 0.95rem;
}

.brand .subtitle strong {
  color: var(--accent);
  font-weight: 600;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.banner {
  display: none;
  margin-bottom: 14px;
  padding: 12px 14px;
  border-radius: var(--radius);
  background: #3a2208;
  border: 1px solid #a86b1a;
  color: #ffe6b8;
  font-weight: 600;
}

.banner.visible {
  display: block;
}

.banner code {
  background: rgba(0, 0, 0, 0.35);
  padding: 2px 6px;
  border-radius: 6px;
  font-size: 0.9em;
}

.panel {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
}

.panel h2 {
  margin: 0 0 10px;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
}

.phrase-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
}

.phrase-btn {
  min-height: var(--tap-min);
  padding: 12px 10px;
  border: 2px solid var(--border);
  border-radius: 12px;
  background: var(--btn);
  color: var(--text);
  font-size: clamp(1rem, 2.8vw, 1.15rem);
  font-weight: 700;
  text-align: center;
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease, transform 0.08s ease;
  touch-action: manipulation;
  user-select: none;
}

.phrase-btn:hover {
  background: var(--btn-hover);
  border-color: var(--accent-dim);
}

.phrase-btn:active,
.phrase-btn.pressed {
  background: var(--btn-active);
  border-color: var(--accent);
  transform: scale(0.97);
}

.phrase-btn:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

.phrase-btn.speaking {
  border-color: var(--success);
  box-shadow: 0 0 0 2px rgba(61, 220, 151, 0.35);
}

.freeform label {
  display: block;
  margin-bottom: 8px;
  font-weight: 700;
  font-size: 1.05rem;
}

.freeform textarea {
  width: 100%;
  min-height: 96px;
  resize: vertical;
  padding: 14px;
  border-radius: 12px;
  border: 2px solid var(--border);
  background: var(--bg-panel);
  color: var(--text);
  font-size: 1.15rem;
  line-height: 1.4;
}

.freeform textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(76, 201, 240, 0.25);
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 10px;
}

.btn {
  min-height: var(--tap-min);
  min-width: 110px;
  padding: 12px 18px;
  border: none;
  border-radius: 12px;
  font-size: 1.1rem;
  font-weight: 800;
  cursor: pointer;
  touch-action: manipulation;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: transform 0.08s ease, filter 0.12s ease;
}

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

.btn:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  transform: none;
}

.btn-primary {
  background: var(--accent);
  color: #041018;
}

.btn-primary:hover:not(:disabled) {
  filter: brightness(1.08);
}

.btn-secondary {
  background: var(--btn);
  color: var(--text);
  border: 2px solid var(--border);
}

.btn-danger {
  background: #5a1d1d;
  color: #ffd5d5;
  border: 2px solid #8b2e2e;
}

.btn-danger:hover:not(:disabled) {
  filter: brightness(1.1);
}

.spinner {
  width: 1.1em;
  height: 1.1em;
  border: 2px solid rgba(4, 16, 24, 0.25);
  border-top-color: #041018;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  display: none;
}

.btn.loading .spinner {
  display: inline-block;
}

.btn.loading .btn-label {
  opacity: 0.85;
}

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

.recent {
  margin-top: 12px;
}

.recent h3 {
  margin: 0 0 8px;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

.recent-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.recent-chip {
  min-height: 44px;
  max-width: 100%;
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-panel);
  color: var(--text);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.recent-chip:hover {
  border-color: var(--accent-dim);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%) translateY(120%);
  max-width: min(92vw, 420px);
  padding: 14px 18px;
  border-radius: 12px;
  background: #4a1515;
  border: 1px solid var(--danger);
  color: #ffe8e8;
  font-weight: 700;
  box-shadow: var(--shadow);
  z-index: 100;
  opacity: 0;
  transition: transform 0.25s ease, opacity 0.25s ease;
  pointer-events: none;
}

.toast.visible {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--warning);
  display: inline-block;
}

.status-dot.ok {
  background: var(--success);
}

.status-dot.bad {
  background: var(--danger);
}

.status-label {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.footer-note {
  margin-top: 8px;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.8rem;
}

@media (min-width: 640px) {
  .app {
    padding: 20px 24px 40px;
  }

  .phrase-grid {
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  }
}
