:root {
  --bg: #f5f6f8;
  --panel: #fff;
  --line: #e1e4e8;
  --text: #1f2328;
  --muted: #686e76;
  --accent: #2563eb;
  --accent-bg: #eaf0fe;
  --accent-ink: #fff;
  --ok: #15803d;
  --rec: #dc2626;
  --danger-bg: #fef2f2;
  --danger-ink: #991b1b;
  --danger-line: #fecaca;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html { max-width: 100%; }

body {
  margin: 0;
  min-width: 0;
  color: var(--text);
  background: var(--bg);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

.topbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 12px;
  min-width: 0;
  padding:
    calc(9px + env(safe-area-inset-top))
    calc(16px + env(safe-area-inset-right))
    9px
    calc(16px + env(safe-area-inset-left));
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 3;
}

.topbar h1 {
  margin: 0;
  font-size: 16px;
  line-height: 1.25;
  flex: 1 1 auto;
}

#session-cluster {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-left: auto;
  min-width: 0;
  max-width: 100%;
}

#account-line {
  margin: 0;
  color: var(--muted);
  max-width: 100%;
}

#account-name { overflow-wrap: anywhere; }

h2, h3 { line-height: 1.25; }

h2 { font-size: 16px; margin: 0 0 12px; }

h3 { font-size: 16px; margin: 0 0 8px; }

.login-card {
  width: min(100% - 32px, 40rem);
  margin: 16px auto calc(28px + env(safe-area-inset-bottom));
}

.shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  min-width: 0;
  min-height: calc(100dvh - 52px);
}

aside,
.detail {
  min-width: 0;
  max-width: 100%;
}

aside {
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.library-label {
  margin: 12px 12px 4px;
}

#library-search {
  width: calc(100% - 24px);
  max-width: calc(100% - 24px);
  margin: 0 12px 8px;
}

#library-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 0 12px 10px;
  min-width: 0;
}

#library-list {
  overflow: auto;
  max-height: 40vh;
  min-width: 0;
}

#library-status {
  margin: 0;
  padding: 8px 14px 12px;
  color: var(--muted);
  overflow-wrap: anywhere;
}

#library-more {
  width: calc(100% - 24px);
  margin: 0 12px 12px;
}

.detail {
  padding:
    12px
    calc(12px + env(safe-area-inset-right))
    calc(28px + env(safe-area-inset-bottom))
    calc(12px + env(safe-area-inset-left));
}

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 14px 16px;
  margin-bottom: 12px;
  min-width: 0;
}

.note, #file-hint, #chosen, #job-line, #empty-hint, #audio-status, #audio-until, .meta, #detail-empty {
  color: var(--muted);
  overflow-wrap: anywhere;
  max-width: 100%;
  min-width: 0;
}

#detail-empty { margin: 0 0 12px; }

#file-hint, #chosen, .note { margin: 0 0 12px; }

label { display: block; font-weight: 650; margin-bottom: 6px; }

input[type="file"],
input[type="text"],
input[type="search"],
input[type="password"],
select {
  display: block;
  width: 100%;
  max-width: 100%;
  min-width: 0;
  margin-bottom: 8px;
  font: inherit;
}

input[type="text"],
input[type="search"],
input[type="password"],
select {
  min-height: 48px;
  margin-bottom: 12px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: inherit;
}

button, .button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  width: 100%;
  max-width: 100%;
  min-height: 48px;
  padding: 12px 16px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
  font: inherit;
  font-weight: 650;
  text-decoration: none;
  text-align: center;
  cursor: pointer;
}

button.primary, #login-submit, #telegram-login-button, #telegram-link-submit, #submit, #save-categories, #protocol-save, #tpl-save, #replace-confirm {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

button.danger {
  color: var(--rec);
  background: #fff;
  border-color: var(--line);
}

#delete-confirm {
  background: #fff;
  color: var(--rec);
  border-color: var(--rec);
}

.topbar button,
.titlebar button,
.titlebar .button,
.subline button,
.chip,
.btns button,
.settings-box .head button,
.nav-row,
.cat-row button,
.protocol-actions button,
.doc-actions button {
  width: auto;
  min-width: 0;
}

button:disabled {
  opacity: 0.45;
  cursor: default;
}

:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

#telegram-login { margin: 0 0 12px; }

#telegram-login-button { width: 100%; }

#telegram-link-form {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

#telegram-link-status, #telegram-status {
  margin: 0;
  color: var(--muted);
  overflow-wrap: anywhere;
}

#telegram-status:not(:empty) { margin: 0 0 8px; }

#login-error, #meta-error, #settings-error, #protocol-error, #template-error {
  margin: 0;
  color: var(--danger-ink);
  font-weight: 650;
  overflow-wrap: anywhere;
}

#login-error:not(:empty), #meta-error:not(:empty), #settings-error:not(:empty), #protocol-error:not(:empty), #template-error:not(:empty) { margin-top: 8px; }

#copy-status, #settings-status {
  margin: 8px 0 0;
  color: var(--muted);
  min-height: 1.2em;
  overflow-wrap: anywhere;
}

.titlebar, .subline, .btns, .head, .cat-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.titlebar { margin-bottom: 6px; }

.subline { margin: 0 0 8px; }

.grow { flex: 1 1 100%; }

#record-title, #record-category, #new-category {
  flex: 1 1 12rem;
  min-width: 0;
  margin-bottom: 0;
}

.titlebar button, .titlebar .button, .subline button {
  flex: 1 1 8rem;
}

.chip {
  display: inline-block;
  flex: 0 1 auto;
  max-width: 100%;
  min-height: 32px;
  height: auto;
  padding: 3px 10px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
  line-height: 1.35;
  text-align: center;
  white-space: normal;
  overflow-wrap: anywhere;
}

.chip[aria-pressed="true"] {
  background: var(--accent-bg);
  color: var(--accent);
  border-color: transparent;
}

button.row {
  display: block;
  width: 100%;
  min-height: 48px;
  padding: 9px 14px;
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  background: #fff;
  color: inherit;
  text-align: left;
  font-weight: 600;
}

button.row[aria-pressed="true"] { background: var(--accent-bg); }

button.row:focus-visible {
  outline-offset: -4px;
}

.row .t {
  display: block;
  font-weight: 650;
  overflow-wrap: anywhere;
}

.row .m {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 500;
  overflow-wrap: anywhere;
}

.group {
  padding: 8px 14px 2px;
  font-size: 11px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}

.meta { margin: 0; font-size: 13px; overflow-wrap: anywhere; flex: 1 1 10rem; }

#status { margin: 0; font-weight: 650; }

#job-line { margin: 8px 0 0; overflow-wrap: anywhere; }

#audio-status, #audio-until { margin: 0; overflow-wrap: anywhere; }

#audio-until { margin-top: 6px; }

.progress-block { margin-top: 12px; }

#progress-label {
  display: block;
  margin-bottom: 6px;
  color: var(--muted);
}

progress {
  width: 100%;
  height: 12px;
  accent-color: var(--accent);
}

#recovery {
  background: var(--danger-bg);
  border-color: var(--danger-line);
}

#recovery h2, #alert { color: var(--danger-ink); }

#alert { margin: 0 0 12px; overflow-wrap: anywhere; }

#transcript {
  margin: 0;
  min-height: 8rem;
  max-height: 55vh;
  max-width: 100%;
  overflow: auto;
  padding: 12px;
  border-radius: 10px;
  background: #f8fafc;
  border: 1px solid var(--line);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font: 15px/1.6 ui-monospace, "Cascadia Mono", Consolas, monospace;
}

#empty-hint { margin: 0 0 8px; }

#status.is-done { color: var(--ok); }

#status.is-bad { color: var(--danger-ink); }

.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  background: rgba(0, 0, 0, 0.35);
}

.box {
  width: min(360px, 100%);
  max-height: min(90vh, 100%);
  overflow: auto;
  padding: 18px 20px;
  border-radius: 10px;
  background: #fff;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

.settings-box {
  width: min(900px, 100%);
  padding: 0;
  display: flex;
  flex-direction: column;
}

.settings-box .head {
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  justify-content: space-between;
}

.settings-box .head h2 { margin: 0; }

.settings-body { min-width: 0; }

.nav-row {
  display: block;
  width: 100%;
  min-height: 48px;
  text-align: left;
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  background: #fff;
  color: inherit;
  font-weight: 500;
}

.nav-row[aria-current="page"] {
  background: var(--accent-bg);
  color: var(--accent);
  font-weight: 650;
}

.pane { padding: 16px; min-width: 0; }

.cat-row input { margin-bottom: 0; }

.btns { justify-content: flex-end; margin-top: 12px; }

#delete-message { margin: 0 0 12px; overflow-wrap: anywhere; }

@media (min-width: 800px) {
  .shell {
    grid-template-columns: 300px minmax(0, 1fr);
    height: calc(100dvh - 52px);
    min-height: 0;
  }

  aside {
    width: 300px;
    max-width: 300px;
    border-bottom: 0;
    border-right: 1px solid var(--line);
  }

  #library-list {
    flex: 1;
    max-height: none;
  }

  .detail { overflow: auto; }

  .titlebar button, .titlebar .button, .subline button { flex: 0 0 auto; }

  .settings-body {
    display: grid;
    grid-template-columns: minmax(8rem, 11rem) minmax(0, 1fr);
    min-height: 0;
  }
}

@media (max-width: 799px) {
  .titlebar, .subline, .cat-row { align-items: stretch; }
  .titlebar button, .titlebar .button, .subline button, .cat-row button { flex: 1 1 auto; }
  .protocol-actions button, .doc-actions button { flex: 1 1 100%; }
}

textarea {
  display: block;
  width: 100%;
  max-width: 100%;
  min-width: 0;
  min-height: 6rem;
  margin-bottom: 12px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: inherit;
  font: inherit;
  resize: vertical;
  overflow-wrap: anywhere;
}

.protocol-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 0 12px;
  min-width: 0;
}

.protocol-grid .wide { grid-column: 1 / -1; }

.protocol-actions,
.doc-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  min-width: 0;
  margin: 8px 0;
}

.protocol-actions select { width: auto; min-width: 0; flex: 1 1 12rem; margin-bottom: 0; }

button.link {
  width: auto;
  border: 0;
  background: transparent;
  color: var(--accent);
  padding: 4px 0;
}

.sec h4 {
  margin: 12px 0 4px;
  font-size: 12px;
  font-weight: 650;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
  overflow-wrap: anywhere;
}

#protocol-status, #template-status { margin: 0; overflow-wrap: anywhere; }

#replace-message, #template-delete-message { margin: 0 0 12px; overflow-wrap: anywhere; }

@media (min-width: 800px) {
  .protocol-grid { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
}

#live-level-bar {
  height: 8px;
  margin: 8px 0;
  background: var(--line);
  border-radius: 999px;
  overflow: hidden;
}

#live-level-fill {
  display: block;
  height: 100%;
  width: 0;
  background: var(--rec);
}

#live-preview {
  max-height: 8rem;
  overflow: auto;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

#live-status, #live-level, #live-duration { overflow-wrap: anywhere; }

/* The private panel asset supplies its controls; app rules preserve the shell. */
#speakers-card, #speakers-root { min-width:0; max-width:100%; overflow-wrap:anywhere; }
#speakers-root:empty { display:none; }
#speakers-card .speakers-panel { padding:0; }
#speakers-card .speakers-bar button { width:auto; flex:0 1 auto; }
#speakers-card .speaker-name { width:auto; min-height:44px; max-width:100%; }
@media (max-width:400px) {
  #speakers-card .speakers-bar > button, #speakers-card .speakers-bar > label { flex:1 1 100%; }
}
