:root {
  --bg: #0f1020;
  --bg-2: #181a35;
  --card: #20234a;
  --text: #f5f6ff;
  --muted: #a3a7d4;
  --accent: #7c5cff;
  --shadow: rgba(0, 0, 0, 0.45);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: radial-gradient(1200px 800px at 20% -10%, #2a2d63 0%, var(--bg) 55%);
  color: var(--text);
  min-height: 100vh;
  /* Dynamic viewport height keeps the layout correct with mobile browser bars. */
  min-height: 100dvh;
  display: flex;
  justify-content: center;
  /* Respect iOS notch / home indicator when installed as a PWA. */
  padding: max(32px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
    max(32px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  -webkit-text-size-adjust: 100%;
}

.app {
  width: 100%;
  max-width: 920px;
}

.tabs {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 28px;
}

.tab {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  border: none;
  background: var(--bg-2);
  color: var(--muted);
  font-size: 1rem;
  font-weight: 600;
  padding: 12px 18px;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, transform 0.08s ease;
  user-select: none;
}

.tab:hover {
  color: var(--text);
}

.tab.active {
  background: var(--accent);
  color: #fff;
}

.tab-rename {
  font-size: 0.8rem;
  opacity: 0;
  transition: opacity 0.15s ease;
}

.tab:hover .tab-rename {
  opacity: 0.7;
}

.tab-rename:hover {
  opacity: 1;
}

.tab-del {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  font-size: 0.7rem;
  background: rgba(0, 0, 0, 0.25);
  opacity: 0;
  transition: opacity 0.15s ease, background 0.15s ease;
}

.tab:hover .tab-del {
  opacity: 0.8;
}

.tab-del:hover {
  opacity: 1 !important;
  background: #ff4b4b;
  color: #fff;
}

.tab-name-input {
  border: none;
  border-radius: 8px;
  padding: 2px 8px;
  font-size: 1rem;
  font-weight: 600;
  width: 9ch;
  background: rgba(0, 0, 0, 0.25);
  color: #fff;
}

.tab-name-input:focus {
  outline: 2px solid rgba(255, 255, 255, 0.6);
}

.tab.editing .tab-rename,
.tab.editing .tab-del {
  display: none;
}

.tab-add {
  border: 2px dashed #3a3e76;
  background: transparent;
  color: var(--muted);
  font-size: 1.2rem;
  line-height: 1;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease;
}

.tab-add:hover {
  color: var(--text);
  border-color: var(--accent);
}

.tab-panel {
  display: none;
}

.tab-panel.active {
  display: block;
}

.header {
  text-align: center;
  margin-bottom: 24px;
}

.header h1 {
  font-size: 2.4rem;
  letter-spacing: -0.5px;
}

.subtitle {
  color: var(--muted);
  margin-top: 6px;
}

.controls {
  display: flex;
  justify-content: center;
  margin-bottom: 28px;
}

.volume {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--bg-2);
  padding: 10px 18px;
  border-radius: 999px;
  color: var(--muted);
  box-shadow: 0 6px 20px var(--shadow);
}

.volume input[type="range"] {
  accent-color: var(--accent);
  width: 180px;
  cursor: pointer;
}

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

.pad {
  position: relative;
  border: none;
  border-radius: 18px;
  padding: 26px 16px;
  min-height: 120px;
  color: var(--text);
  font-size: 1.05rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: var(--card);
  box-shadow: 0 8px 22px var(--shadow);
  transition: transform 0.08s ease, box-shadow 0.15s ease, filter 0.15s ease;
  user-select: none;
}

.pad:hover {
  filter: brightness(1.12);
  transform: translateY(-2px);
}

.pad:active,
.pad.active {
  transform: translateY(2px) scale(0.97);
  box-shadow: 0 3px 10px var(--shadow);
}

.pad .emoji {
  font-size: 2rem;
  line-height: 1;
}

.pad .key {
  position: absolute;
  top: 10px;
  right: 10px;
  font-size: 0.72rem;
  font-weight: 700;
  background: rgba(0, 0, 0, 0.35);
  padding: 3px 8px;
  border-radius: 7px;
  color: var(--text);
}

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

/* Recording buttons and recorder */
.pad .delete {
  position: absolute;
  top: 8px;
  left: 8px;
  width: 24px;
  height: 24px;
  border: none;
  border-radius: 7px;
  background: rgba(0, 0, 0, 0.35);
  color: var(--text);
  font-size: 0.85rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.15s ease;
}

.pad:hover .delete {
  opacity: 1;
}

.pad .delete:hover {
  background: #ff4b4b;
}

.recorder {
  margin-top: 36px;
  background: var(--bg-2);
  border-radius: 18px;
  padding: 24px;
  box-shadow: 0 8px 22px var(--shadow);
}

.recorder h2 {
  font-size: 1.15rem;
  margin-bottom: 16px;
}

.recorder-row {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}

.record-btn,
.save-btn,
.discard-btn {
  border: none;
  border-radius: 12px;
  padding: 12px 20px;
  font-size: 1rem;
  font-weight: 600;
  color: #fff;
  cursor: pointer;
  transition: filter 0.15s ease, transform 0.08s ease;
}

.record-btn { background: #e9436b; }
.save-btn { background: #21c7a8; }
.discard-btn { background: #555a8a; }

.record-btn:hover,
.save-btn:hover,
.discard-btn:hover { filter: brightness(1.12); }

.record-btn.recording {
  background: #ff4b4b;
  animation: pulse 1s infinite;
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 75, 75, 0.5); }
  50% { box-shadow: 0 0 0 10px rgba(255, 75, 75, 0); }
}

.record-status {
  color: var(--muted);
}

.save-area {
  margin-top: 20px;
}

.preview {
  width: 100%;
  margin-bottom: 14px;
}

.save-row {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.save-row input[type="text"] {
  flex: 1;
  min-width: 160px;
  background: var(--card);
  border: 1px solid #34386b;
  border-radius: 12px;
  padding: 12px 16px;
  color: var(--text);
  font-size: 1rem;
}

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

.pad .edit {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 24px;
  height: 24px;
  border: none;
  border-radius: 7px;
  background: rgba(0, 0, 0, 0.35);
  color: var(--text);
  font-size: 0.85rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.15s ease;
}

.pad:hover .edit {
  opacity: 1;
}

.pad .edit:hover {
  background: var(--accent);
}

.pad .key {
  position: absolute;
  bottom: 8px;
  right: 10px;
  top: auto;
}

/* Countdown overlay (before recording starts) */
.countdown-overlay {
  position: fixed;
  inset: 0;
  background: rgba(8, 9, 22, 0.78);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.countdown-overlay[hidden] {
  display: none;
}

.countdown-ring {
  position: relative;
  width: 240px;
  height: 240px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.countdown-ring svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  /* Let the glow spill out instead of being clipped to a square. */
  overflow: visible;
  /* Start the progress arc at the top and grow clockwise. */
  transform: rotate(-90deg);
}

.countdown-track {
  fill: none;
  stroke: rgba(255, 255, 255, 0.12);
  stroke-width: 8;
}

.countdown-progress {
  fill: none;
  stroke: var(--accent);
  stroke-width: 8;
  stroke-linecap: round;
  /* 2 * pi * r (r = 54) ≈ 339.292 */
  stroke-dasharray: 339.292;
  stroke-dashoffset: 339.292;
  filter: drop-shadow(0 0 12px rgba(124, 92, 255, 0.85));
}

.countdown-ring.run .countdown-progress {
  animation: countdown-fill var(--countdown-duration, 3s) linear forwards;
}

@keyframes countdown-fill {
  to { stroke-dashoffset: 0; }
}

.countdown-number {
  position: relative;
  font-size: 6rem;
  font-weight: 800;
  line-height: 1;
  color: #fff;
  text-shadow: 0 4px 30px rgba(124, 92, 255, 0.8);
}

.countdown-number.pop {
  animation: countdown-pop 1s ease-out;
}

@keyframes countdown-pop {
  0% { transform: scale(0.4); opacity: 0; }
  30% { transform: scale(1.15); opacity: 1; }
  60% { transform: scale(1); }
  100% { transform: scale(1); opacity: 1; }
}

.record-btn.counting {
  background: #f6a609;
}

/* Edit modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 50;
}

.modal[hidden] {
  display: none;
}

.modal-box {
  background: var(--bg-2);
  border-radius: 18px;
  padding: 28px;
  width: 100%;
  max-width: 420px;
  box-shadow: 0 20px 60px var(--shadow);
}

.modal-box h2 {
  margin-bottom: 20px;
  font-size: 1.3rem;
}

.field {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 18px;
}

.field > span {
  width: 80px;
  color: var(--muted);
  font-weight: 600;
}

.field input[type="text"] {
  flex: 1;
  background: var(--card);
  border: 1px solid #34386b;
  border-radius: 12px;
  padding: 12px 16px;
  color: var(--text);
  font-size: 1rem;
}

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

.shortcut-btn {
  flex: 1;
  background: var(--card);
  border: 1px solid #34386b;
  border-radius: 12px;
  padding: 12px 16px;
  color: var(--text);
  font-size: 1rem;
  cursor: pointer;
  text-align: left;
}

.shortcut-btn.listening {
  border-color: var(--accent);
  color: var(--accent);
}

.shortcut-clear {
  border: none;
  background: var(--card);
  color: var(--muted);
  border-radius: 10px;
  width: 38px;
  height: 38px;
  cursor: pointer;
  font-size: 0.9rem;
}

.shortcut-clear:hover {
  background: #ff4b4b;
  color: #fff;
}

.modal-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 24px;
  gap: 12px;
}

.modal-actions-right {
  display: flex;
  gap: 12px;
}

/* On touch devices there is no hover, so controls that normally appear on
   hover (edit/delete on pads, tab rename/delete) must stay visible. */
@media (hover: none) {
  .pad .edit,
  .pad .delete {
    opacity: 0.85;
  }
  .tab-del,
  .tab-rename {
    opacity: 0.7;
  }
}

/* Tablets and smaller */
@media (max-width: 720px) {
  .board {
    grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
    gap: 12px;
  }
  .pad {
    min-height: 100px;
    padding: 20px 12px;
    font-size: 1rem;
  }
  .header h1 { font-size: 2rem; }
  .recorder { padding: 18px; }
}

/* Phones */
@media (max-width: 480px) {
  .tabs {
    gap: 8px;
    margin-bottom: 20px;
  }
  .tab {
    padding: 9px 14px;
    font-size: 0.9rem;
  }
  .tab-add {
    width: 40px;
    height: 40px;
  }
  .header { margin-bottom: 18px; }
  .header h1 { font-size: 1.7rem; }

  .controls { margin-bottom: 20px; }
  .volume {
    width: 100%;
    justify-content: space-between;
  }
  .volume input[type="range"] { flex: 1; width: auto; }

  .board {
    grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
    gap: 10px;
  }
  .pad {
    min-height: 88px;
    padding: 16px 8px;
    border-radius: 14px;
    font-size: 0.9rem;
  }
  .pad .emoji { font-size: 1.6rem; }
  .pad .label { line-height: 1.15; }

  /* Recorder controls stack and stretch for easy tapping. */
  .record-btn,
  .save-btn,
  .discard-btn {
    padding: 12px 16px;
  }
  .save-row { flex-direction: column; }
  .save-row .save-btn,
  .save-row .discard-btn { width: 100%; }

  /* Edit modal: stack fields and let action buttons fill the width. */
  .modal { padding: 12px; align-items: flex-end; }
  .modal-box {
    padding: 20px;
    max-width: none;
    max-height: 90dvh;
    overflow-y: auto;
    border-radius: 18px 18px 0 0;
  }
  .field {
    flex-wrap: wrap;
    gap: 8px;
  }
  .field > span {
    width: 100%;
  }
  .field input[type="text"] {
    width: 100%;
    flex: 1 1 auto;
  }
  .shortcut-btn {
    flex: 1 1 auto;
    min-width: 0;
  }
  .modal-actions {
    flex-direction: column-reverse;
    align-items: stretch;
  }
  .modal-actions-right {
    flex-direction: column;
  }
  .modal-actions .save-btn,
  .modal-actions .discard-btn {
    width: 100%;
  }

  .countdown-ring {
    width: 180px;
    height: 180px;
  }
  .countdown-number { font-size: 4.5rem; }
}
