/* ==============================================
   即時雙向翻譯 — Mobile-first Dark Theme
   ============================================== */

/* === Custom Properties === */
:root {
  --bg: #0f0f1a;
  --surface: #1a1a2e;
  --surface-hover: #222240;
  --primary: #e94560;
  --primary-dim: #c23152;
  --accent: #4ecdc4;
  --text: #e8e8f0;
  --text-dim: #8888aa;
  --text-muted: #555577;
  --border: #2a2a4a;
  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
  --safe-bottom: env(safe-area-inset-bottom, 16px);
  --safe-top: env(safe-area-inset-top, 0px);
  --header-h: 52px;
  --transition: 0.2s ease;
}

/* === Reset === */
*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC',
    'PingFang TC', 'Microsoft YaHei', sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

#app {
  height: 100%;
  display: flex;
  flex-direction: column;
  max-width: 480px;
  margin: 0 auto;
  position: relative;
}

/* === Header === */
#header {
  display: flex;
  align-items: center;
  padding: 0 16px;
  height: var(--header-h);
  padding-top: var(--safe-top);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 10;
}

#header h1 {
  flex: 1;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.header-actions {
  display: flex;
  gap: 4px;
}

.btn-icon {
  background: none;
  border: none;
  font-size: 20px;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--text-dim);
  transition: background var(--transition), color var(--transition);
}

.btn-icon:active {
  background: var(--surface-hover);
  color: var(--text);
}

/* === Main === */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 12px;
  padding-bottom: var(--safe-bottom);
  min-height: 0;
}

/* === No-Speech Notice === */
.no-speech-notice {
  display: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  margin-bottom: 4px;
  text-align: center;
  color: var(--text-dim);
  font-size: 13px;
  line-height: 1.7;
}

.no-speech .no-speech-notice {
  display: block;
}

.no-speech .mic-btn,
.no-speech .status-indicator,
.no-speech .btn-mode {
  display: none !important;
}

/* === Panels === */
.panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
  position: relative;
  min-height: 0;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.lang-select {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 28px 6px 10px;
  font-size: 14px;
  font-weight: 500;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M3 5l3 3 3-3' fill='none' stroke='%238888aa' stroke-width='1.5' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  cursor: pointer;
  max-width: 160px;
}

.lang-select:focus {
  outline: none;
  border-color: var(--primary);
}

.btn-mode {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  width: 36px;
  height: 36px;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--text-dim);
  transition: all var(--transition);
}

.btn-mode:active,
.panel[data-mode="text"] .btn-mode {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}

/* === Panel Body === */
.panel-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 12px 16px;
  min-height: 0;
  overflow-y: auto;
}

.transcript {
  width: 100%;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.transcript-original {
  font-size: 20px;
  font-weight: 500;
  line-height: 1.4;
  word-break: break-word;
  color: var(--text);
  min-height: 28px;
  transition: color 0.15s;
}

.transcript-original:empty::after {
  content: '點擊下方麥克風開始';
  color: var(--text-muted);
  font-size: 14px;
  font-weight: 400;
}

.transcript-translated {
  font-size: 15px;
  line-height: 1.5;
  word-break: break-word;
  color: var(--accent);
  min-height: 22px;
}

.transcript-translated:empty {
  display: none;
}

/* Text input (hidden in voice mode) */
.text-input {
  display: none;
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 15px;
  font-family: inherit;
  line-height: 1.5;
  resize: none;
  transition: border-color var(--transition);
}

.text-input:focus {
  outline: none;
  border-color: var(--primary);
}

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

/* Text input — send button group */
.input-group {
  display: none;
  width: 100%;
  gap: 0;
}

.panel[data-mode="text"] .input-group {
  display: flex;
}

.input-group .text-input {
  flex: 1;
  display: block;
  border-radius: var(--radius-sm) 0 0 var(--radius-sm);
  border-right: none;
}

.btn-send {
  width: 48px;
  min-height: 48px;
  border: 1px solid var(--border);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  background: var(--bg);
  color: var(--text-dim);
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all var(--transition);
}

.btn-send:active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.panel[data-mode="text"] .text-input {
  display: block;
}

/* No-speech: always show input-group */
.no-speech .input-group {
  display: flex !important;
}

/* === Panel Footer === */
.panel-footer {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 10px 14px 16px;
  flex-shrink: 0;
}

/* === Mic Button === */
.mic-btn {
  width: 68px;
  height: 68px;
  border-radius: 50%;
  border: 3px solid var(--primary);
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  cursor: pointer;
  transition: all 0.15s ease;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  position: relative;
}

/* Ripple ring */
.mic-btn::after {
  content: '';
  position: absolute;
  inset: -6px;
  border-radius: 50%;
  background: transparent;
  transition: background 0.15s ease;
}

.mic-btn:active::after,
.panel[data-status="listening"] .mic-btn::after {
  background: rgba(233, 69, 96, 0.15);
}

.mic-btn:active,
.panel[data-status="listening"] .mic-btn {
  background: var(--primary);
  border-color: var(--primary);
  transform: scale(1.05);
}

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

.mic-btn:disabled::after {
  background: transparent;
}

.mic-btn:disabled:active {
  background: var(--bg);
  border-color: var(--primary);
}

.mic-icon {
  font-size: 24px;
  line-height: 1;
}

.mic-hint {
  font-size: 9px;
  font-weight: 500;
  opacity: 0.8;
  letter-spacing: 0.5px;
}

/* === Status Indicator === */
.status-indicator {
  font-size: 11px;
  color: var(--text-dim);
  height: 16px;
  display: flex;
  align-items: center;
}

.status-indicator span {
  display: none;
}

.panel[data-status="idle"] .status-idle {
  display: inline;
}

.panel[data-status="listening"] .status-listening {
  display: inline;
  color: var(--primary);
  animation: pulse 1.5s ease-in-out infinite;
}

.panel[data-status="processing"] .status-processing {
  display: inline;
  color: var(--accent);
}

/* Processing spinner */
.panel[data-status="processing"] .mic-btn {
  border-style: dashed;
  animation: spin 1.4s linear infinite;
}

/* === Swap Button === */
.swap-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 50%;
  width: 32px;
  height: 32px;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--text-dim);
  align-self: center;
  flex-shrink: 0;
  transition: all var(--transition);
  z-index: 2;
  margin: -4px 0;
}

.swap-btn:active {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
  transform: rotate(180deg);
}

/* === Overlay === */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
  z-index: 50;
}

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

/* === Drawer === */
.drawer {
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%) translateY(100%);
  width: 100%;
  max-width: 480px;
  background: var(--surface);
  border-radius: var(--radius) var(--radius) 0 0;
  display: flex;
  flex-direction: column;
  max-height: 65vh;
  z-index: 60;
  transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
  box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.4);
}

.drawer.open {
  transform: translateX(-50%) translateY(0);
}

.drawer-header {
  display: flex;
  align-items: center;
  padding: 16px 16px 12px;
  flex-shrink: 0;
}

.drawer-header h2 {
  flex: 1;
  font-size: 16px;
  font-weight: 600;
}

.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 0 16px 16px;
  -webkit-overflow-scrolling: touch;
}

.drawer-footer {
  padding: 12px 16px;
  padding-bottom: calc(12px + var(--safe-bottom));
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

/* === History Items === */
.history-empty {
  text-align: center;
  color: var(--text-muted);
  padding: 40px 0;
  font-size: 14px;
}

.history-item {
  padding: 14px 0;
  border-bottom: 1px solid var(--border);
  animation: fadeIn 0.3s ease;
}

.history-item:last-child {
  border-bottom: none;
}

.history-time {
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.history-text {
  font-size: 14px;
  line-height: 1.5;
  word-break: break-word;
}

.history-text.original {
  color: var(--text);
  font-weight: 500;
}

.history-text.translated {
  color: var(--accent);
  font-size: 13px;
}

.history-meta {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 4px;
}

/* === Buttons === */
.btn-danger {
  width: 100%;
  padding: 12px;
  background: transparent;
  color: var(--primary);
  border: 1px solid var(--primary);
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
}

.btn-danger:active {
  background: var(--primary);
  color: white;
}

/* === Animations === */
@keyframes pulse {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.4;
  }
}

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

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

/* === Landscape === */
@media (orientation: landscape) {
  #main {
    flex-direction: row;
    gap: 8px;
  }

  .panel {
    flex: 1;
  }

  .swap-btn {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%) rotate(90deg);
    margin: 0;
  }

  .swap-btn:active {
    transform: translate(-50%, -50%) rotate(270deg);
  }
}

/* === Short Screens === */
@media (max-height: 600px) {
  .panel-body {
    padding: 6px 12px;
  }

  .transcript-original {
    font-size: 16px;
  }

  .transcript-translated {
    font-size: 13px;
  }

  .mic-btn {
    width: 56px;
    height: 56px;
    border-width: 2.5px;
  }

  .mic-icon {
    font-size: 20px;
  }

  .panel-footer {
    padding: 6px 14px 10px;
  }
}

/* === Scrollbar === */
::-webkit-scrollbar {
  width: 4px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}
