:root {
  --bg: #050816;
  --bg-alt: #0b1020;
  --fg: #e5e7eb;
  --accent: #38bdf8;
  --accent-soft: rgba(56, 189, 248, 0.15);
  --accent-purple: #a78bfa;
  --accent-purple-soft: rgba(167, 139, 250, 0.2);
  --neutral: #64748b;
  --neutral-soft: rgba(100, 116, 139, 0.25);
  --danger: #f97373;
  --border-subtle: rgba(148, 163, 184, 0.4);
  --muted: #9ca3af;
  --radius-lg: 16px;
  --radius-md: 12px;
  --radius-sm: 8px;
  --shadow-soft: 0 22px 45px rgba(15, 23, 42, 0.85);
  --shadow-subtle: 0 1px 0 rgba(148, 163, 184, 0.2);
  --shadow-hover: 0 8px 24px rgba(15, 23, 42, 0.5);
  --focus-ring: 0 0 0 2px rgba(56, 189, 248, 0.5);
  --focus-ring-purple: 0 0 0 2px rgba(167, 139, 250, 0.5);
  --transition: 0.25s ease;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI",
    sans-serif;
  background: radial-gradient(circle at top, #0f172a 0, #020617 55%, #000 100%);
  color: var(--fg);
  display: flex;
  align-items: stretch;
  justify-content: center;
  padding: 24px;
}

.app-main,
.app-header,
.app-footer {
  max-width: 840px;
  width: 100%;
}

.app-header {
  text-align: center;
  margin-bottom: 24px;
}

.app-header h1 {
  margin: 0 0 6px;
  font-size: clamp(1.9rem, 2.3vw, 2.4rem);
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.subtitle {
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.app-main {
  background: radial-gradient(circle at top left, rgba(56, 189, 248, 0.12), transparent 50%),
    radial-gradient(circle at bottom right, rgba(167, 139, 250, 0.08), transparent 50%),
    linear-gradient(145deg, #020617, #020617 42%, #020617);
  border-radius: 20px;
  box-shadow: var(--shadow-soft);
  border: 1px solid rgba(148, 163, 184, 0.25);
  padding: 28px 24px 24px;
  display: flex;
  flex-direction: column;
  gap: 22px;
}

.panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* Mode toggle */
.mode-panel {
  flex-direction: row;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.mode-toggle {
  display: inline-flex;
  border-radius: 999px;
  padding: 4px;
  background: rgba(15, 23, 42, 0.95);
  border: 1px solid var(--border-subtle);
  box-shadow: var(--shadow-subtle);
  transition: box-shadow var(--transition), border-color var(--transition);
}

.mode-btn {
  border: none;
  border-radius: 999px;
  padding: 8px 18px;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  background: transparent;
  color: var(--muted);
  transition: color var(--transition), background var(--transition),
    box-shadow var(--transition), transform 0.2s ease;
}

.mode-btn:hover {
  color: var(--fg);
}

.mode-btn.active {
  background: linear-gradient(135deg, #22c1c3, #38bdf8);
  color: #020617;
  box-shadow: 0 2px 10px rgba(56, 189, 248, 0.4);
}

.mode-toggle .mode-btn:nth-child(2).active {
  background: linear-gradient(135deg, #8b5cf6, #a78bfa);
  box-shadow: 0 2px 10px rgba(167, 139, 250, 0.4);
}

.mode-btn:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

.mode-btn.active:focus-visible {
  box-shadow: var(--focus-ring), 0 2px 10px rgba(56, 189, 248, 0.4);
}

/* Buttons */
.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 6px;
}

.btn {
  border-radius: 999px;
  font-size: 0.9rem;
  font-weight: 500;
  padding: 10px 20px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  cursor: pointer;
  border: 1px solid transparent;
  transition: transform 0.2s ease, box-shadow 0.2s ease,
    background-color 0.2s ease, border-color 0.2s ease, color 0.2s ease;
}

.btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
}

.btn:active {
  transform: translateY(0);
  transition-duration: 0.1s;
}

.btn:focus-visible {
  outline: none;
}

/* Primary – Decode: bright blue, white text */
.btn-primary {
  background: linear-gradient(135deg, #0ea5e9, #38bdf8);
  color: #ffffff;
  border-color: rgba(56, 189, 248, 0.4);
  box-shadow: 0 4px 14px rgba(56, 189, 248, 0.4);
}

.btn-primary:hover {
  background: linear-gradient(135deg, #0284c7, #0ea5e9);
  box-shadow: 0 6px 22px rgba(56, 189, 248, 0.5);
}

.btn-primary:focus-visible {
  box-shadow: var(--focus-ring), 0 4px 14px rgba(56, 189, 248, 0.4);
}

/* Secondary – Encode: purple, white text */
.btn-secondary {
  background: linear-gradient(135deg, #7c3aed, #a78bfa);
  color: #ffffff;
  border-color: rgba(167, 139, 250, 0.4);
  box-shadow: 0 4px 14px rgba(167, 139, 250, 0.35);
}

.btn-secondary:hover {
  background: linear-gradient(135deg, #6d28d9, #8b5cf6);
  box-shadow: 0 6px 22px rgba(167, 139, 250, 0.45);
}

.btn-secondary:focus-visible {
  box-shadow: var(--focus-ring-purple), 0 4px 14px rgba(167, 139, 250, 0.35);
}

/* Neutral – Clear: gray background */
.btn-neutral {
  background: #475569;
  color: #f1f5f9;
  border-color: rgba(148, 163, 184, 0.4);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.2);
}

.btn-neutral:hover {
  background: #64748b;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
}

.btn-neutral:focus-visible {
  box-shadow: var(--focus-ring);
}

/* Ghost – Copy: dark outlined, subtle hover highlight */
.btn-ghost {
  background: transparent;
  color: var(--fg);
  border: 1px solid rgba(148, 163, 184, 0.5);
  box-shadow: none;
}

.btn-ghost:hover {
  background: rgba(30, 41, 59, 0.6);
  border-color: rgba(148, 163, 184, 0.6);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
}

.btn-ghost:focus-visible {
  box-shadow: var(--focus-ring);
}

.btn.copied {
  color: #34d399;
  border-color: rgba(52, 211, 153, 0.6);
}

/* Play Morse: outline style, disabled when no output */
.btn-play {
  background: transparent;
  color: var(--accent);
  border: 1px solid rgba(56, 189, 248, 0.5);
  box-shadow: none;
}

.btn-play:hover:not(:disabled) {
  background: rgba(56, 189, 248, 0.15);
  border-color: rgba(56, 189, 248, 0.7);
  box-shadow: 0 4px 12px rgba(56, 189, 248, 0.2);
}

.btn-play:focus-visible {
  box-shadow: var(--focus-ring);
}

.btn-play:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Fields */
.field-label {
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--muted);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.textarea,
.output {
  width: 100%;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-subtle);
  background: rgba(15, 23, 42, 0.92);
  color: var(--fg);
  font-size: 0.96rem;
  padding: 14px 16px;
  line-height: 1.5;
  resize: vertical;
  min-height: 160px;
  max-height: 300px;
  box-shadow: var(--shadow-subtle);
  transition: border-color var(--transition), box-shadow var(--transition),
    background-color var(--transition);
}

.textarea::placeholder {
  color: rgba(148, 163, 184, 0.75);
}

.textarea:focus,
.textarea:focus-visible,
.output:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: var(--focus-ring);
  background: #020617;
}

.output {
  min-height: 120px;
  max-height: 260px;
  white-space: pre-wrap;
  word-wrap: break-word;
}

/* Messages */
.messages {
  min-height: 22px;
  font-size: 0.86rem;
}

.message {
  padding: 10px 14px;
  border-radius: var(--radius-md);
  display: inline-flex;
  align-items: flex-start;
  gap: 8px;
  border: 1px solid transparent;
  transition: opacity var(--transition);
}

.message.info {
  background: var(--accent-soft);
  border-color: rgba(56, 189, 248, 0.4);
  color: #e0f2fe;
}

.message.error {
  background: rgba(248, 113, 113, 0.12);
  border-color: rgba(248, 113, 113, 0.6);
  color: #fee2e2;
}

.message .label {
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-size: 0.75rem;
}

.message .body {
  font-size: 0.86rem;
}

/* History */
.history-panel {
  margin-top: 8px;
  padding-top: 20px;
  border-top: 1px solid rgba(148, 163, 184, 0.18);
}

.history-title {
  margin: 0 0 12px;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--muted);
  letter-spacing: 0.04em;
}

.history-list {
  margin: 0;
  padding: 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-height: 220px;
  overflow-y: auto;
}

.history-item {
  padding: 14px 16px;
  border-radius: var(--radius-md);
  background: rgba(15, 23, 42, 0.7);
  border: 1px solid rgba(148, 163, 184, 0.2);
  font-size: 0.88rem;
  color: var(--fg);
  word-break: break-word;
  display: flex;
  flex-direction: column;
  gap: 6px;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.history-item:hover {
  border-color: rgba(148, 163, 184, 0.35);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.history-item .arrow {
  display: block;
  color: var(--accent);
  font-weight: 600;
  margin: 2px 0;
  font-size: 0.8rem;
}

.history-item.history-empty {
  color: var(--muted);
  font-style: italic;
  justify-content: center;
}

.app-footer {
  margin-top: 20px;
  text-align: center;
  color: var(--muted);
  font-size: 0.8rem;
}

/* Responsive */
@media (max-width: 640px) {
  body {
    padding: 16px;
  }

  .app-header {
    margin-bottom: 18px;
  }

  .app-main {
    padding: 20px 16px 18px;
    gap: 18px;
  }

  .mode-panel {
    flex-direction: column;
    align-items: flex-start;
  }

  .mode-toggle {
    width: 100%;
    justify-content: stretch;
  }

  .mode-btn {
    flex: 1;
  }

  .controls {
    gap: 10px;
    justify-content: flex-start;
  }

  .btn {
    flex: 1 1 140px;
    justify-content: center;
  }

  .textarea,
  .output {
    padding: 12px 14px;
  }

  .history-panel {
    padding-top: 16px;
  }

  .history-list {
    max-height: 180px;
    gap: 10px;
  }

  .history-item {
    padding: 12px 14px;
  }
}
