:root {
  color-scheme: light dark;
  --bg: #f6f7f2;
  --panel: #ffffff;
  --text: #121512;
  --muted: #68736e;
  --line: rgba(18, 21, 18, 0.14);
  --source: #0f766e;
  --target: #7c3aed;
  --danger: #dc2626;
  --danger-dark: #991b1b;
  --button: #164e63;
  --button-dark: #083344;
  --shadow: 0 18px 50px rgba(12, 28, 24, 0.18);
  font-family:
    ui-sans-serif,
    system-ui,
    -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    sans-serif;
}

* {
  box-sizing: border-box;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  clip-path: inset(50%);
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  background:
    linear-gradient(180deg, rgba(15, 118, 110, 0.08), transparent 34%),
    var(--bg);
  color: var(--text);
}

button,
select,
input {
  font: inherit;
}

button {
  -webkit-tap-highlight-color: transparent;
}

.app-shell {
  width: 100%;
  height: 100dvh;
  min-height: 100%;
  position: relative;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 14px;
  padding:
    max(16px, env(safe-area-inset-top))
    max(16px, env(safe-area-inset-right))
    max(18px, env(safe-area-inset-bottom))
    max(16px, env(safe-area-inset-left));
}

.top-bar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 44px minmax(0, 1fr);
  gap: 10px;
  align-items: end;
  padding-right: 52px;
}

.field,
.key-field,
.setting-field,
.toggle-field {
  display: grid;
  gap: 6px;
  min-width: 0;
}

.field span,
.key-field span,
.setting-field span,
.toggle-field span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.settings-note {
  margin: 0;
  color: var(--muted);
  line-height: 1.5;
  font-size: 14px;
}

select,
input {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.88);
  color: var(--text);
  padding: 0 12px;
  outline: none;
}

select:focus,
input:focus {
  border-color: var(--source);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.16);
}

input[type="checkbox"] {
  width: 20px;
  min-height: 20px;
  accent-color: var(--source);
}

.toggle-field {
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
}

.settings-group {
  display: grid;
  gap: 12px;
  margin: 0;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.settings-group legend {
  padding: 0 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.icon-button {
  width: 44px;
  height: 42px;
  display: inline-grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.86);
  color: var(--text);
  cursor: pointer;
}

.icon-button span {
  display: block;
  line-height: 1;
  font-size: 20px;
}

.settings-fab {
  position: absolute;
  top: max(16px, env(safe-area-inset-top));
  right: max(16px, env(safe-area-inset-right));
  z-index: 3;
}

.transcript-panel {
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 4px 2px;
  scrollbar-width: none;
}

.transcript-panel::-webkit-scrollbar {
  display: none;
}

.face-panel {
  min-height: 0;
  display: grid;
  grid-template-rows: minmax(0, 1fr) minmax(0, 1fr);
  gap: 10px;
}

.face-panel[hidden] {
  display: none;
}

.face-side {
  min-height: 0;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.86);
  overflow: hidden;
}

.face-target {
  border-top: 4px solid var(--target);
}

.face-source {
  border-bottom: 4px solid var(--source);
}

.face-content {
  width: 100%;
  max-height: 100%;
  display: grid;
  gap: 10px;
  padding: 18px;
  text-align: center;
}

.face-target .face-content {
  transform: rotate(180deg);
}

.face-language {
  color: var(--muted);
  font-size: 12px;
  font-weight: 900;
}

.face-content p {
  margin: 0;
  color: var(--text);
  font-size: clamp(24px, 7vw, 44px);
  font-weight: 850;
  line-height: 1.25;
  word-break: keep-all;
  overflow-wrap: anywhere;
}

.app-shell.face-mode .transcript-panel {
  display: none;
}

.app-shell:not(.face-mode) .face-panel {
  display: none;
}

.diagnostics-panel {
  min-height: 112px;
  display: grid;
  gap: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.74);
  padding: 10px;
}

.diagnostics-panel[hidden] {
  display: none;
}

.diagnostic-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
  align-items: center;
  text-align: center;
}

.diagnostic-item {
  min-width: 0;
  display: grid;
  gap: 4px;
}

.diagnostic-item span {
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
}

.diagnostic-item strong {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 12px;
}

.event-log {
  height: min(18vh, 132px);
  min-height: 66px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin: 0;
  padding: 0;
  list-style: none;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.3;
}

.event-log li {
  word-break: break-word;
}

.empty-state {
  margin: auto;
  color: var(--muted);
  font-size: 15px;
  font-weight: 700;
}

.bubble {
  width: min(86%, 680px);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.9);
  box-shadow: 0 8px 22px rgba(12, 28, 24, 0.08);
  padding: 12px;
}

.bubble.source {
  align-self: flex-start;
  border-left: 4px solid var(--source);
}

.bubble.language-mismatch {
  border-style: dashed;
  border-left-color: #d97706;
  opacity: 0.86;
}

.bubble.language-mismatch .bubble-meta {
  color: #d97706;
}

.bubble.target {
  align-self: flex-end;
  border-right: 4px solid var(--target);
}

.bubble.image-result {
  width: min(94%, 760px);
  align-self: center;
  display: grid;
  gap: 10px;
  border-top: 4px solid #0891b2;
}

.image-preview {
  width: 100%;
  max-height: min(30vh, 260px);
  object-fit: contain;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.08);
}

.image-source-text,
.image-notes {
  margin: 0;
  color: var(--muted);
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.45;
  font-size: 13px;
}

.image-translated-text {
  font-weight: 800;
}

.bubble-meta {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  margin-bottom: 6px;
}

.bubble-text {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.45;
  font-size: 16px;
}

.control-zone {
  display: grid;
  justify-items: center;
  gap: 10px;
  padding-top: 4px;
}

.button-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(14px, 5vw, 26px);
}

.tool-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: min(100%, 280px);
}

.primary-button {
  width: clamp(92px, 26vw, 130px);
  aspect-ratio: 1;
  border: 0;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 28%, #2dd4bf, var(--button) 62%, var(--button-dark));
  color: #ffffff;
  box-shadow: var(--shadow);
  cursor: pointer;
  font-size: clamp(18px, 5vw, 24px);
  font-weight: 900;
  letter-spacing: 0;
  transition:
    transform 160ms ease,
    filter 160ms ease,
    background 200ms ease;
}

.primary-button:active {
  transform: scale(0.97);
}

.primary-button:disabled {
  cursor: wait;
  filter: saturate(0.55);
}

.primary-button.running {
  background: radial-gradient(circle at 35% 28%, #fb7185, var(--danger) 58%, var(--danger-dark));
  animation: pulse 1.35s ease-in-out infinite;
}

.primary-button.connecting {
  background: radial-gradient(circle at 35% 28%, #67e8f9, #0891b2 58%, #155e75);
}

.ptt-button {
  width: clamp(76px, 20vw, 104px);
  aspect-ratio: 1;
  border: 1px solid rgba(15, 118, 110, 0.34);
  border-radius: 50%;
  background: radial-gradient(circle at 35% 28%, #f8fafc, #d9f99d 58%, #65a30d);
  color: #1f2937;
  box-shadow: var(--shadow);
  cursor: pointer;
  font-size: clamp(15px, 4vw, 20px);
  font-weight: 900;
  letter-spacing: 0;
  transition:
    transform 160ms ease,
    filter 160ms ease,
    background 200ms ease;
}

.ptt-button:active {
  transform: scale(0.97);
}

.ptt-button:disabled {
  cursor: wait;
  filter: saturate(0.55);
}

.ptt-button.recording {
  background: radial-gradient(circle at 35% 28%, #fda4af, #e11d48 58%, #9f1239);
  color: #ffffff;
  animation: pulse 1.35s ease-in-out infinite;
}

.ptt-button.sending {
  background: radial-gradient(circle at 35% 28%, #fde68a, #d97706 58%, #92400e);
  color: #ffffff;
}

.tool-button,
.clear-button {
  min-width: 68px;
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.72);
  color: var(--muted);
  cursor: pointer;
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0;
}

.tool-button {
  background: rgba(15, 118, 110, 0.12);
  color: var(--source);
  border-color: rgba(15, 118, 110, 0.28);
}

.tool-button.busy {
  cursor: wait;
  filter: saturate(0.7);
}

.tool-button:disabled,
.clear-button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.status-line {
  min-height: 20px;
  margin: 0;
  color: var(--muted);
  text-align: center;
  font-size: 13px;
  font-weight: 700;
}

.settings-dialog {
  width: min(calc(100vw - 28px), 420px);
  max-height: min(calc(100dvh - 28px), 720px);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  padding: 0;
  box-shadow: var(--shadow);
}

.settings-dialog::backdrop {
  background: rgba(10, 18, 20, 0.48);
}

.settings-form {
  display: grid;
  gap: 18px;
  padding: 18px;
  max-height: inherit;
  overflow-y: auto;
}

.settings-form header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.settings-form h1 {
  margin: 0;
  font-size: 20px;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.secondary-button,
.save-button {
  min-width: 74px;
  min-height: 42px;
  border-radius: 8px;
  border: 1px solid var(--line);
  cursor: pointer;
  font-weight: 800;
}

.secondary-button {
  background: transparent;
  color: var(--muted);
}

.save-button {
  background: var(--source);
  color: #ffffff;
  border-color: var(--source);
}

@keyframes pulse {
  0%,
  100% {
    transform: scale(1);
    box-shadow: 0 18px 50px rgba(220, 38, 38, 0.28);
  }

  50% {
    transform: scale(1.035);
    box-shadow: 0 24px 64px rgba(220, 38, 38, 0.38);
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101412;
    --panel: #171c19;
    --text: #eef3ef;
    --muted: #9ba7a0;
    --line: rgba(238, 243, 239, 0.16);
    --shadow: 0 18px 50px rgba(0, 0, 0, 0.38);
  }

  select,
  input,
  .icon-button,
  .tool-button,
  .clear-button,
  .bubble,
  .face-side {
    background: rgba(23, 28, 25, 0.92);
  }

  .diagnostics-panel {
    background: rgba(23, 28, 25, 0.74);
  }
}

@media (max-height: 640px) {
  .app-shell {
    gap: 10px;
  }

  .primary-button {
    width: clamp(82px, 21vh, 105px);
    font-size: clamp(17px, 4vh, 22px);
  }

  .ptt-button {
    width: clamp(68px, 18vh, 88px);
    font-size: clamp(14px, 3.6vh, 18px);
  }

  .bubble-text {
    font-size: 15px;
  }

  .face-content p {
    font-size: clamp(22px, 6vh, 36px);
  }
}
