/* === 黔音留痕 / global === */
:root {
  --bg: #0f1419;
  --bg-elev: #1a2029;
  --bg-elev-2: #232a36;
  --border: #2a3340;
  --text: #e6edf3;
  --text-dim: #8b98a5;
  --text-mute: #5d6b78;
  --primary: #d97706;        /* 山土黄 */
  --primary-hover: #f59e0b;
  --accent: #14b8a6;         /* 苗银青 */
  --danger: #ef4444;
  --warn: #f59e0b;
  --ok: #10b981;
  --hl: #fbbf24;             /* 高亮当前词 */
  --hl-bg: rgba(251, 191, 36, 0.18);
  --radius: 8px;
  --radius-lg: 12px;
  --shadow: 0 4px 24px rgba(0,0,0,0.35);
  --mono: ui-monospace, SFMono-Regular, "JetBrains Mono", Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
               "Microsoft YaHei", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  text-size-adjust: 100%;
  -webkit-text-size-adjust: 100%;
}

body {
  display: flex;
  flex-direction: column;
}

h1, h2, h3 { margin: 0; font-weight: 600; }
button { font: inherit; cursor: pointer; }
input, select, textarea {
  font: inherit;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius);
  padding: 8px 10px;
  outline: none;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--primary);
}

/* === topbar === */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  display: inline-flex;
  align-items: center; justify-content: center;
  width: 36px; height: 36px;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  border-radius: 8px;
  font-weight: 700;
  font-size: 20px;
  color: #fff;
}
.brand h1 { font-size: 18px; }
.subtitle { color: var(--text-dim); font-size: 12px; }

.topbar-actions { display: flex; gap: 12px; align-items: center; }
.preset-switch { display: flex; align-items: center; gap: 8px; color: var(--text-dim); }
.preset-switch select { min-width: 160px; }

/* === buttons === */
.btn {
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  color: var(--text);
  padding: 8px 14px;
  border-radius: var(--radius);
  transition: 0.15s;
  display: inline-flex; align-items: center; gap: 6px;
}
.btn:hover { background: #2c3441; }
.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-ghost { background: transparent; }
.btn-block { width: 100%; justify-content: center; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.recording {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
  animation: pulse 1.4s infinite;
}
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(239,68,68,0.7); }
  50% { box-shadow: 0 0 0 8px rgba(239,68,68,0); }
}

/* === layout === */
.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 280px 1fr 320px;
  gap: 1px;
  background: var(--border);
  overflow: hidden;
  min-height: 0;
}
.panel {
  background: var(--bg);
  padding: 16px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
}
.panel h2 {
  font-size: 14px;
  color: var(--text-dim);
  margin-bottom: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* === source panel === */
.dropzone {
  border: 2px dashed var(--border);
  border-radius: var(--radius-lg);
  padding: 24px 12px;
  text-align: center;
  cursor: pointer;
  transition: 0.15s;
  margin-bottom: 16px;
}
.dropzone:hover, .dropzone.dragover {
  border-color: var(--primary);
  background: rgba(217, 119, 6, 0.06);
}
.dz-icon { font-size: 32px; margin-bottom: 8px; }
.dz-title { font-weight: 500; }
.dz-hint { color: var(--text-mute); font-size: 12px; margin-top: 4px; }

.recorder { margin-bottom: 16px; }
.rec-status {
  margin-top: 8px;
  text-align: center;
  font-size: 12px;
  color: var(--text-dim);
  font-family: var(--mono);
}

.player { margin-top: auto; }
.player audio {
  width: 100%;
  filter: invert(0.9);
}
.audio-meta {
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 8px;
  word-break: break-all;
}

/* === transcript panel === */
.transcript-toolbar {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 12px;
}
.toolbar-actions { display: flex; gap: 8px; }

.transcript {
  flex: 1;
  overflow-y: auto;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px 24px;
  font-size: 16px;
  line-height: 2.0;
  scroll-behavior: smooth;
}
.transcript .placeholder {
  color: var(--text-dim);
  font-size: 14px;
  line-height: 1.8;
}
.transcript .placeholder ol { padding-left: 20px; }
.transcript .placeholder li { margin: 6px 0; }

.transcript .seg {
  margin: 0 0 12px;
  padding: 4px 6px;
  border-radius: 6px;
}
.transcript .seg.active-seg {
  background: rgba(20,184,166,0.06);
}
.transcript .seg-meta {
  display: inline-block;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-mute);
  margin-right: 8px;
  user-select: none;
}
.transcript .w {
  cursor: pointer;
  padding: 1px 1px;
  border-radius: 3px;
  transition: background 0.05s linear;
}
.transcript .w:hover { background: rgba(20,184,166,0.18); }
.transcript .w.active {
  background: var(--hl-bg);
  color: var(--hl);
  font-weight: 500;
}
.transcript .w.evolved {
  border-bottom: 2px solid var(--accent);
}
.transcript[contenteditable="true"] {
  outline: 2px solid var(--primary);
  outline-offset: -2px;
}
.transcript[contenteditable="true"] .w { cursor: text; }

/* === evolution panel === */
.evolution-stats {
  display: flex;
  gap: 12px;
  margin-bottom: 16px;
}
.stat {
  flex: 1;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  text-align: center;
}
.stat-num {
  font-size: 24px;
  font-weight: 600;
  color: var(--primary);
  font-family: var(--mono);
}
.stat-label {
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 4px;
}

.rule-add {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 6px;
  align-items: center;
  margin-bottom: 12px;
}
.rule-add input { width: 100%; }
.rule-add .arrow { color: var(--text-mute); text-align: center; }
.rule-add .btn {
  grid-column: 1 / -1;
  margin-top: 6px;
}

.rule-list-title {
  font-size: 12px;
  color: var(--text-dim);
  margin: 8px 0 6px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  display: flex;
  align-items: center;
  gap: 6px;
}
.rule-count-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 22px;
  height: 18px;
  padding: 0 6px;
  background: var(--primary);
  color: #fff;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
  font-family: var(--mono);
}
.rule-list {
  list-style: none;
  margin: 0; padding: 0;
  overflow-y: auto;
  flex: 1 1 auto;
  min-height: 120px;
}
.rule-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.rule-list .rule-text {
  flex: 1;
  word-break: break-all;
}
.rule-list .rule-text del {
  color: var(--text-mute);
  text-decoration: line-through;
}
.rule-list .rule-text ins {
  color: var(--accent);
  text-decoration: none;
  font-weight: 500;
}
.rule-list .rule-meta {
  font-size: 11px;
  color: var(--text-mute);
  font-family: var(--mono);
}
.rule-list .rule-edit,
.rule-list .rule-del {
  border: none;
  background: transparent;
  color: var(--text-mute);
  cursor: pointer;
  padding: 2px 6px;
  font-size: 14px;
  border-radius: 4px;
}
.rule-list .rule-edit:hover { color: var(--accent); background: rgba(20,184,166,0.1); }
.rule-list .rule-del:hover { color: var(--danger); background: rgba(239,68,68,0.1); }

.rule-list li.editing {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  grid-template-areas:
    "pattern arrow replacement"
    "actions actions actions";
  gap: 6px;
  background: var(--bg-elev-2);
  padding: 10px 8px;
  border-radius: var(--radius);
}
.rule-list li.editing .rule-edit-pattern { grid-area: pattern; width: 100%; }
.rule-list li.editing .arrow { grid-area: arrow; align-self: center; color: var(--text-mute); }
.rule-list li.editing .rule-edit-replacement { grid-area: replacement; width: 100%; }
.rule-list li.editing .rule-save,
.rule-list li.editing .rule-cancel {
  font-size: 12px;
  padding: 6px 12px;
}
.rule-list li.editing .rule-save { grid-area: actions; justify-self: end; }
.rule-list li.editing .rule-cancel {
  grid-area: actions;
  justify-self: end;
  margin-right: 88px;
}

@media (max-width: 640px) {
  .rule-list li.editing {
    grid-template-columns: 1fr;
    grid-template-areas:
      "pattern"
      "replacement"
      "actions";
  }
  .rule-list li.editing .arrow { display: none; }
  .rule-list li.editing .rule-cancel { margin-right: 88px; }
}
.rule-list .empty {
  color: var(--text-mute);
  text-align: center;
  padding: 24px 8px;
  font-size: 12px;
}

/* === statusbar === */
.statusbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 20px;
  background: var(--bg-elev);
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-dim);
}
.status-item.ok { color: var(--ok); }
.status-item.err { color: var(--danger); }
.status-item.warn { color: var(--warn); }
.spacer { flex: 1; }

/* === drawer === */
.mask {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  justify-content: flex-end;
  z-index: 50;
}
.mask.hidden { display: none; }
.drawer {
  width: 480px;
  max-width: 100%;
  background: var(--bg-elev);
  height: 100%;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow);
  animation: slidein 0.2s ease;
}
@keyframes slidein {
  from { transform: translateX(100%); }
  to { transform: translateX(0); }
}
.drawer-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
}
.drawer .hint { color: var(--text-dim); font-size: 12px; margin: 0 0 16px; }

.form-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 12px;
}
.form-row label {
  font-size: 12px;
  color: var(--text-dim);
}
.advanced {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 12px;
  margin: 12px 0;
}
.advanced summary {
  cursor: pointer;
  padding: 4px;
  color: var(--text-dim);
}
.drawer-actions {
  display: flex;
  gap: 8px;
  margin-top: 16px;
}

.presets-title {
  font-size: 13px;
  color: var(--text-dim);
  margin: 24px 0 8px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.preset-list {
  list-style: none; margin: 0; padding: 0;
}
.preset-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 6px;
}
.preset-list li.active {
  border-color: var(--primary);
  background: rgba(217,119,6,0.08);
}
.preset-list .preset-info {
  flex: 1; overflow: hidden;
}
.preset-list .preset-name { font-weight: 500; }
.preset-list .preset-meta {
  font-size: 11px;
  color: var(--text-mute);
  font-family: var(--mono);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.preset-list .preset-actions {
  display: flex; gap: 4px;
}
.preset-list .preset-actions .btn {
  padding: 4px 10px;
  font-size: 12px;
}

/* === toasts === */
.toasts {
  position: fixed;
  top: 70px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 100;
  pointer-events: none;
}
.toast {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-left: 4px solid var(--primary);
  padding: 10px 14px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  min-width: 240px;
  max-width: 380px;
  animation: toast-in 0.2s ease;
}
.toast.ok { border-left-color: var(--ok); }
.toast.err { border-left-color: var(--danger); }
.toast.warn { border-left-color: var(--warn); }
@keyframes toast-in {
  from { transform: translateX(20px); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* === responsive === */
@media (max-width: 1100px) {
  .layout { grid-template-columns: 240px 1fr 280px; }
}

/* 平板：仍是双/三栏堆叠，layout 内部滚动 */
@media (max-width: 900px) and (min-width: 641px) {
  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto;
    overflow-y: auto;
  }
  .panel { max-height: none; min-height: auto; }
  .panel-transcript { min-height: 50vh; }
  .transcript { min-height: 320px; }
  .rule-list { max-height: 50vh; }
}

/* === 手机：整页文档自然滚动 === */
@media (max-width: 640px) {
  html, body {
    height: auto;
    min-height: 100dvh;
    font-size: 15px;
  }
  body {
    display: block;
    /* iPhone 底部安全区 */
    padding-bottom: env(safe-area-inset-bottom);
  }

  /* 顶栏：吸顶 */
  .topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    padding: 10px 14px;
    padding-top: calc(10px + env(safe-area-inset-top));
    flex-wrap: wrap;
    gap: 10px;
  }
  .brand { gap: 8px; min-width: 0; flex: 1 1 auto; }
  .brand h1 { font-size: 16px; }
  .subtitle { display: none; }
  .logo { width: 32px; height: 32px; font-size: 18px; }

  .topbar-actions {
    gap: 8px;
    width: 100%;
    flex-wrap: nowrap;
  }
  .preset-switch { flex: 1; min-width: 0; }
  .preset-switch span { display: none; }
  .preset-switch select {
    width: 100%;
    min-width: 0;
    height: 40px;
    padding: 0 10px;
  }
  #open-settings { flex: 0 0 auto; }

  /* layout：脱离 grid 高度限制，整页滚动 */
  .layout {
    display: block;
    overflow: visible;
    height: auto;
    background: transparent;
    gap: 0;
  }
  .panel {
    max-height: none;
    min-height: auto;
    overflow: visible;
    padding: 14px;
    border-bottom: 1px solid var(--border);
  }
  .panel-source { order: 1; }
  .panel-transcript { order: 2; }
  .panel-evolution { order: 3; }

  .panel h2 {
    font-size: 12px;
    margin-bottom: 10px;
  }

  /* 触屏友好按钮 */
  .btn {
    padding: 10px 14px;
    min-height: 42px;
    font-size: 14px;
  }

  /* 音源面板 */
  .dropzone { padding: 22px 12px; }
  .dz-icon { font-size: 28px; }
  .player audio {
    width: 100%;
    height: 40px;
  }

  /* 转写面板 */
  .transcript-toolbar {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }
  .transcript-toolbar h2 { margin-bottom: 0; }
  .toolbar-actions {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
    width: 100%;
  }
  .toolbar-actions .btn {
    justify-content: center;
    padding: 10px 6px;
    font-size: 13px;
  }
  .transcript {
    padding: 14px 16px;
    font-size: 16px;
    line-height: 2.0;
    max-height: 60vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  /* 词点击区放大，方便手指点 */
  .transcript .w {
    padding: 3px 1px;
    margin: 0 1px;
    border-radius: 4px;
  }

  /* 进化中心 */
  .evolution-stats { gap: 8px; }
  .stat { padding: 10px 8px; }
  .stat-num { font-size: 22px; }
  .stat-label { font-size: 11px; }

  .rule-add {
    grid-template-columns: 1fr;
    gap: 8px;
  }
  .rule-add .arrow { display: none; }
  .rule-add input {
    padding: 11px 12px;
    font-size: 16px;  /* 16px 防 iOS 自动放大 */
  }
  .rule-add .btn {
    grid-column: 1;
    margin-top: 0;
  }

  .rule-list-title { margin-top: 12px; }
  .rule-list { max-height: none; }
  .rule-list li {
    flex-wrap: wrap;
    padding: 12px 10px;
    gap: 6px;
  }
  .rule-list .rule-text {
    font-size: 15px;
    flex: 1 1 100%;
    line-height: 1.5;
  }
  .rule-list .rule-meta { font-size: 11px; }
  .rule-list .rule-edit,
  .rule-list .rule-del {
    padding: 6px 10px;
    font-size: 16px;
  }

  /* 编辑态：actions 区改用 flex 横排 */
  .rule-list li.editing {
    grid-template-columns: 1fr;
    grid-template-areas:
      "pattern"
      "replacement"
      "actions";
    padding: 12px 10px;
  }
  .rule-list li.editing .arrow { display: none; }
  .rule-list li.editing .rule-edit-pattern,
  .rule-list li.editing .rule-edit-replacement {
    padding: 11px 12px;
    font-size: 16px;
  }
  .rule-list li.editing .rule-save,
  .rule-list li.editing .rule-cancel {
    grid-area: actions;
    justify-self: stretch;
    margin: 0;
    padding: 10px 16px;
    font-size: 14px;
  }
  .rule-list li.editing .rule-save {
    justify-self: end;
    min-width: 80px;
  }
  .rule-list li.editing .rule-cancel {
    justify-self: start;
    min-width: 80px;
  }

  /* 状态栏：3 行栅格 */
  .statusbar {
    position: sticky;
    bottom: 0;
    z-index: 10;
    padding: 10px 12px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
    gap: 8px 12px;
    display: grid;
    grid-template-columns: 1fr 1fr;
  }
  .status-item { font-size: 12px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  #status-api { grid-column: 1 / 2; }
  #status-task { grid-column: 2 / 3; text-align: right; }
  .statusbar .spacer { display: none; }
  #export-md {
    grid-column: 1 / -1;
    width: 100%;
    justify-content: center;
    padding: 12px;
    min-height: 44px;
  }

  /* 抽屉：全屏 */
  .drawer {
    width: 100%;
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .drawer-header { padding: 14px 16px; }
  .drawer-body { padding: 16px; }
  .form-row input {
    padding: 11px 12px;
    font-size: 16px;
  }
  .drawer-actions {
    flex-direction: column;
  }
  .drawer-actions .btn { width: 100%; justify-content: center; }
  .preset-list li { flex-wrap: wrap; }
  .preset-list .preset-info { flex: 1 1 100%; min-width: 0; }
  .preset-list .preset-actions { flex: 1 1 100%; justify-content: flex-end; }

  /* Toast：底部、避开 statusbar */
  .toasts {
    top: auto;
    bottom: calc(80px + env(safe-area-inset-bottom));
    right: 12px;
    left: 12px;
  }
  .toast { max-width: none; min-width: 0; }
}

/* 极窄屏（小手机 / iPhone SE） */
@media (max-width: 380px) {
  #open-settings { font-size: 0; padding: 0 12px; }
  #open-settings::before { content: "⚙"; font-size: 18px; }
  .toolbar-actions .btn { font-size: 12px; padding: 9px 4px; }
  .stat-label { font-size: 10px; }
  .stat-num { font-size: 20px; }
  .brand h1 { font-size: 15px; }
}

/* 横屏手机：减少顶部/底部高度 */
@media (max-width: 900px) and (orientation: landscape) and (max-height: 480px) {
  .topbar { padding: 6px 14px; }
  .panel { padding: 10px; }
  .transcript { max-height: 50vh; }
}

/* 触屏：去掉 hover 残留高亮 */
@media (hover: none) {
  .btn:hover { background: var(--bg-elev-2); }
  .btn-primary:hover { background: var(--primary); border-color: var(--primary); }
  .btn-ghost:hover { background: transparent; }
  .dropzone:hover { border-color: var(--border); background: transparent; }
  .transcript .w:hover { background: transparent; }
  .rule-list .rule-edit:hover,
  .rule-list .rule-del:hover { background: transparent; }
}
