:root {
    --primary: #0f766e;
    --primary-strong: #115e59;
    --primary-soft: #e7f4f1;
    --action: #b94728;
    --action-hover: #97351f;
    --danger: #bd3d3d;
    --canvas: #f3f6f5;
    --surface: #ffffff;
    --surface-soft: #f8faf9;
    --ink: #14221d;
    --muted: #61706a;
    --muted-light: #8b9893;
    --border: #d9e2de;
    --border-strong: #c2d0ca;
    --focus: rgba(15, 118, 110, 0.2);
    --sidebar-width: 224px;
    --voice-width: 390px;
    --radius: 8px;
    --radius-sm: 6px;
    --shadow: 0 10px 30px rgba(20, 34, 29, 0.08);
}

* { box-sizing: border-box; }
html { min-height: 100%; background: var(--canvas); }
body {
    min-width: 320px;
    min-height: 100vh;
    margin: 0;
    overflow-x: hidden;
    color: var(--ink);
    background: var(--canvas);
    font-family: Inter, "PingFang SC", "Microsoft YaHei", system-ui, -apple-system, sans-serif;
    font-size: 14px;
    line-height: 1.5;
    letter-spacing: 0;
}

button, input, select { font: inherit; letter-spacing: 0; }
button, a { touch-action: manipulation; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.52; }
button:focus-visible, input:focus-visible, select:focus-visible, a:focus-visible {
    outline: 3px solid var(--focus);
    outline-offset: 2px;
}
svg { width: 20px; height: 20px; stroke-width: 1.8; }

.skip-link {
    position: fixed;
    z-index: 100;
    top: 8px;
    left: 8px;
    min-height: 44px;
    display: flex;
    align-items: center;
    padding: 8px 13px;
    color: #fff;
    background: var(--primary-strong);
    border-radius: var(--radius-sm);
    transform: translateY(-150%);
}
.skip-link:focus { transform: translateY(0); }

.app-shell { min-height: 100vh; }
.mode-sidebar {
    position: fixed;
    z-index: 20;
    inset: 0 auto 0 0;
    width: var(--sidebar-width);
    display: flex;
    flex-direction: column;
    padding: 18px 14px;
    color: #dcece6;
    background: var(--primary-strong);
}

.brand {
    min-height: 54px;
    display: flex;
    align-items: center;
    gap: 11px;
    padding: 0 7px;
    color: #fff;
    text-decoration: none;
}
.brand-mark {
    width: 38px;
    height: 38px;
    display: grid;
    place-items: center;
    flex: 0 0 auto;
    color: var(--primary-strong);
    background: #efc35c;
    border-radius: 7px;
}
.brand-copy strong, .brand-copy small { display: block; }
.brand-copy strong { font-size: 15px; }
.brand-copy small { margin-top: 1px; color: #b9d3ca; font-size: 11px; }

.mode-navigation { display: grid; gap: 8px; margin-top: 28px; }
.nav-label { padding: 0 8px 4px; color: #9fc1b5; font-size: 10px; font-weight: 750; }
.mode-button {
    min-height: 68px;
    display: grid;
    grid-template-columns: 38px minmax(0, 1fr) 18px;
    align-items: center;
    gap: 9px;
    padding: 8px 10px;
    color: #c9dfd7;
    background: transparent;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    text-align: left;
    transition: color 180ms ease, background-color 180ms ease, border-color 180ms ease;
}
.mode-button:hover { color: #fff; background: rgba(255,255,255,0.08); }
.mode-button.is-active { color: #fff; background: rgba(255,255,255,0.14); border-color: rgba(255,255,255,0.12); }
.mode-icon { width: 36px; height: 36px; display: grid; place-items: center; border-radius: var(--radius-sm); background: rgba(255,255,255,0.09); }
.mode-button.is-active .mode-icon { color: var(--primary-strong); background: #efc35c; }
.mode-button strong, .mode-button small { display: block; }
.mode-button strong { font-size: 13px; }
.mode-button small { margin-top: 3px; color: #9fc1b5; font-size: 10px; }
.mode-button.is-active small { color: #d4e6e0; }
.mode-check { visibility: hidden; width: 17px; height: 17px; }
.mode-button.is-active .mode-check { visibility: visible; }

.sidebar-footer { margin-top: auto; }
.sidebar-settings {
    width: 100%;
    min-height: 46px;
    display: flex;
    align-items: center;
    gap: 9px;
    padding: 0 11px;
    color: #dcece6;
    background: transparent;
    border: 1px solid rgba(255,255,255,0.16);
    border-radius: var(--radius-sm);
    font-weight: 680;
}
.sidebar-settings:hover { color: #fff; background: rgba(255,255,255,0.08); border-color: rgba(255,255,255,0.28); }
.version-label { margin-top: 12px; color: #88afa1; font-size: 10px; text-align: center; }

.workspace { min-height: 100vh; margin-left: var(--sidebar-width); }
.topbar {
    min-height: 76px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 18px;
    padding: 11px 22px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}
.page-title-row, .topbar-status, .network-state, .panel-title,
.avatar-state, .stage-controls, .control-group, .connection-controls,
.voice-title, .session-status-row, .relay-status,
.history-header, .settings-header, .settings-actions {
    display: flex;
    align-items: center;
}
.page-title-row { gap: 10px; }
.topbar h1 { margin: 0; font-size: 20px; line-height: 1.25; }
.topbar p { margin: 4px 0 0; color: var(--muted); font-size: 12px; }
.mode-badge { min-height: 25px; display: inline-flex; align-items: center; padding: 0 9px; color: var(--primary-strong); background: var(--primary-soft); border-radius: 13px; font-size: 10px; font-weight: 750; }
.topbar-status { gap: 12px; }
.network-state { gap: 6px; color: var(--primary); font-size: 12px; font-weight: 650; }
.network-state svg { width: 16px; height: 16px; }
.connection-badge { min-height: 30px; display: inline-flex; align-items: center; gap: 7px; padding: 0 10px; border-radius: 15px; font-size: 11px; font-weight: 700; }
.connection-badge.is-offline { color: #596660; background: #edf1ef; }
.connection-badge.is-online { color: #8d311f; background: #fff0ea; }
.status-dot { width: 7px; height: 7px; display: inline-block; flex: 0 0 auto; border-radius: 50%; background: #98a59f; }
.is-online .status-dot, .avatar-state.is-online .status-dot, .listening-state.is-active .status-dot { background: var(--action); }

.content-layout { display: grid; grid-template-columns: minmax(0, 1fr) var(--voice-width); gap: 16px; padding: 16px; align-items: start; }
.stage-panel, .voice-panel { overflow: hidden; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.panel-header { min-height: 55px; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 0 16px; border-bottom: 1px solid var(--border); }
.panel-title { gap: 8px; font-weight: 760; }
.panel-title svg { width: 18px; height: 18px; color: var(--primary); }
.avatar-state { gap: 6px; color: var(--muted); font-size: 11px; }

.stage-view { position: relative; width: 100%; aspect-ratio: 16 / 9; overflow: hidden; background: #153a32; }
.stage-view > img { width: 100%; height: 100%; display: block; object-fit: cover; object-position: center; }
.stage-id { position: absolute; top: 14px; left: 14px; display: flex; align-items: center; gap: 8px; padding: 7px 9px; color: #fff; background: rgba(10,28,22,0.82); border: 1px solid rgba(255,255,255,0.18); border-radius: var(--radius-sm); }
.avatar-dot { width: 30px; height: 30px; display: grid; place-items: center; color: var(--primary-strong); background: #efc35c; border-radius: 50%; }
.avatar-dot svg { width: 17px; height: 17px; }
.stage-id strong, .stage-id small { display: block; }
.stage-id strong { font-size: 12px; }
.stage-id small { margin-top: 1px; color: rgba(255,255,255,0.72); font-size: 10px; }
.subtitle { position: absolute; left: 50%; bottom: 18px; width: min(680px, calc(100% - 36px)); padding: 9px 14px; color: #fff; background: rgba(8,20,16,0.8); border-radius: 5px; font-size: 14px; line-height: 1.55; text-align: center; transform: translateX(-50%); }

.stage-controls { min-height: 70px; justify-content: space-between; gap: 12px; padding: 11px 14px; }
.control-group, .connection-controls { gap: 8px; }
.control-group-end { justify-content: flex-end; }
.icon-button { width: 44px; height: 44px; display: inline-grid; place-items: center; flex: 0 0 auto; padding: 0; color: var(--ink); background: #fff; border: 1px solid var(--border-strong); border-radius: var(--radius-sm); transition: color 180ms ease, background-color 180ms ease, border-color 180ms ease; }
.icon-button:hover { color: var(--primary); background: var(--primary-soft); border-color: #9fc6bb; }
.icon-button.is-active { color: #fff; background: var(--primary); border-color: var(--primary); }
.icon-button.is-recording { color: #fff; background: var(--danger); border-color: var(--danger); }
.button { min-height: 44px; display: inline-flex; align-items: center; justify-content: center; gap: 7px; padding: 0 15px; border: 1px solid transparent; border-radius: var(--radius-sm); font-size: 13px; font-weight: 720; transition: color 180ms ease, background-color 180ms ease, border-color 180ms ease; }
.button svg { width: 18px; height: 18px; }
.button-primary { color: #fff; background: var(--action); border-color: var(--action); }
.button-primary:hover { background: var(--action-hover); border-color: var(--action-hover); }
.button-danger { color: #fff; background: var(--danger); border-color: var(--danger); }
.button-quiet { color: var(--ink); background: #fff; border-color: var(--border-strong); }
.button-quiet:hover { color: var(--primary); background: var(--primary-soft); border-color: #9fc6bb; }
.button-compact { min-height: 44px; padding: 0 10px; font-size: 11px; }

.voice-panel { position: sticky; top: 16px; }
.voice-header { min-height: 72px; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 11px 13px 10px 16px; border-bottom: 1px solid var(--border); }
.voice-title { gap: 7px; font-size: 15px; font-weight: 780; }
.voice-title svg { width: 18px; height: 18px; color: var(--action); }
.voice-session { padding: 15px; }
.session-status-row { justify-content: space-between; gap: 12px; }
.eyebrow { color: var(--primary); font-size: 10px; font-weight: 780; }
.listening-state { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 10px; }

.microphone-zone { display: flex; flex-direction: column; align-items: center; padding: 20px 0 16px; text-align: center; }
.microphone-button { position: relative; width: 78px; height: 78px; display: grid; place-items: center; color: #fff; background: var(--action); border: 0; border-radius: 50%; box-shadow: 0 9px 22px rgba(185,71,40,0.22); transition: background-color 180ms ease, box-shadow 180ms ease; }
.microphone-button:hover { background: var(--action-hover); }
.microphone-button svg { position: relative; z-index: 2; width: 29px; height: 29px; }
.microphone-button.is-listening { background: var(--primary); box-shadow: 0 0 0 8px rgba(15,118,110,0.1), 0 9px 22px rgba(15,118,110,0.2); }
.mic-rings { position: absolute; inset: -1px; border: 1px solid rgba(255,255,255,0.35); border-radius: 50%; }
.microphone-button.is-listening .mic-rings { animation: mic-pulse 1.6s ease-out infinite; }
.microphone-zone > strong { margin-top: 11px; font-size: 14px; }
.microphone-zone > span:last-child { margin-top: 3px; color: var(--muted); font-size: 10px; }

.waveform-shell { height: 68px; display: flex; align-items: center; padding: 0 10px; background: var(--surface-soft); border: 1px solid var(--border); border-radius: var(--radius-sm); }
.waveform { width: 100%; height: 45px; display: flex; align-items: center; justify-content: center; gap: 3px; overflow: hidden; }
.wave-bar { width: 3px; min-height: 4px; height: 4px; flex: 0 0 auto; background: #aab8b2; border-radius: 2px; transition: height 90ms ease, background-color 180ms ease; }
.waveform.is-active .wave-bar { background: var(--primary); }

.transcript-block { min-height: 82px; margin-top: 11px; padding: 11px 12px; background: #fff; border: 1px solid var(--border); border-left: 3px solid var(--primary); border-radius: var(--radius-sm); }
.transcript-label { color: var(--muted); font-size: 10px; font-weight: 700; }
.transcript-block p { margin: 7px 0 0; color: #43524c; font-size: 12px; line-height: 1.65; }

.echo-options { display: grid; gap: 10px; margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--border); }
.echo-options label { font-size: 11px; font-weight: 700; }
.echo-options select, .settings-drawer input, .settings-drawer select { width: 100%; min-height: 44px; padding: 0 10px; color: var(--ink); background: #fff; border: 1px solid var(--border-strong); border-radius: var(--radius-sm); outline: 0; }
.echo-options select:focus, .settings-drawer input:focus, .settings-drawer select:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--focus); }
.relay-status { justify-content: space-between; gap: 12px; color: var(--muted); font-size: 10px; }
.relay-status strong { color: var(--ink); font-size: 11px; }

.history-panel { border-top: 1px solid var(--border); }
.history-header { justify-content: space-between; gap: 12px; padding: 13px 15px 10px; }
.history-header h2 { margin: 2px 0 0; font-size: 14px; }
.conversation-selector { position: relative; padding: 0 15px 10px; }
.conversation-current { width: 100%; min-height: 52px; display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 7px 10px; color: var(--ink); background: var(--surface-soft); border: 1px solid var(--border); border-radius: var(--radius-sm); text-align: left; }
.conversation-current strong, .conversation-current small { display: block; }
.conversation-current strong { font-size: 11px; }
.conversation-current small { margin-top: 2px; color: var(--muted); font-size: 9px; }
.conversation-current > svg { width: 16px; height: 16px; }
.conversation-menu { position: absolute; z-index: 10; top: calc(100% - 7px); right: 15px; left: 15px; overflow: hidden; background: #fff; border: 1px solid var(--border); border-radius: var(--radius-sm); box-shadow: var(--shadow); }
.conversation-menu button { width: 100%; min-height: 48px; display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 7px 10px; color: var(--ink); background: #fff; border: 0; text-align: left; }
.conversation-menu button + button { border-top: 1px solid var(--border); }
.conversation-menu button:hover { background: var(--primary-soft); }
.conversation-menu small { color: var(--muted); font-size: 9px; }

.message-list { min-height: 195px; max-height: 250px; display: grid; align-content: start; gap: 10px; overflow-y: auto; padding: 6px 15px 15px; }
.message { display: grid; grid-template-columns: 30px minmax(0, 1fr); gap: 8px; align-items: start; }
.message-role { width: 28px; height: 28px; display: grid; place-items: center; border-radius: 50%; font-size: 10px; font-weight: 780; }
.message-user .message-role { color: #fff; background: var(--primary); }
.message-assistant .message-role { color: var(--primary-strong); background: #efc35c; }
.message-role svg { width: 15px; height: 15px; }
.message p { margin: 0; padding: 8px 10px; color: #405049; background: var(--surface-soft); border: 1px solid var(--border); border-radius: var(--radius-sm); font-size: 11px; line-height: 1.6; }
.message-user p { background: var(--primary-soft); border-color: #c5dfd7; }

.toast-region { position: fixed; z-index: 80; right: 20px; bottom: 20px; display: grid; gap: 8px; pointer-events: none; }
.toast { min-width: 260px; display: flex; align-items: center; gap: 9px; padding: 11px 13px; color: #fff; background: #153b32; border-radius: var(--radius-sm); box-shadow: var(--shadow); animation: toast-in 180ms ease both; }
.toast svg { width: 18px; height: 18px; color: #efc35c; }

.drawer-backdrop { position: fixed; z-index: 90; inset: 0; display: flex; justify-content: flex-end; background: rgba(7,18,14,0.55); }
.settings-drawer { width: min(470px, 100%); height: 100%; overflow-y: auto; padding: 20px; background: #fff; box-shadow: -20px 0 55px rgba(7,18,14,0.2); }
.settings-header { justify-content: space-between; gap: 12px; padding-bottom: 15px; border-bottom: 1px solid var(--border); }
.settings-header h2 { margin: 2px 0 0; font-size: 20px; }
.settings-drawer form { padding-top: 16px; }
.settings-drawer fieldset { display: grid; gap: 7px; margin: 0 0 16px; padding: 14px; border: 1px solid var(--border); border-radius: var(--radius-sm); }
.settings-drawer legend { padding: 0 6px; color: var(--primary-strong); font-size: 12px; font-weight: 780; }
.settings-drawer label { margin-top: 5px; font-size: 11px; font-weight: 700; }
.settings-drawer select:disabled { cursor: not-allowed; opacity: 0.62; }
.settings-drawer .settings-help { max-width: 58ch; }
.password-field { display: grid; grid-template-columns: minmax(0, 1fr) 44px; gap: 7px; }
.number-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.settings-help { margin: 0; color: var(--muted); font-size: 10px; line-height: 1.65; }
.settings-actions { justify-content: flex-end; gap: 8px; margin-top: 18px; }

.is-hidden { display: none !important; }

@keyframes mic-pulse {
    0% { opacity: 0.7; transform: scale(1); }
    100% { opacity: 0; transform: scale(1.35); }
}
@keyframes toast-in {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

@media (max-width: 1120px) {
    .content-layout { grid-template-columns: minmax(0, 1fr); }
    .voice-panel { position: static; }
}

@media (max-width: 760px) {
    :root { --sidebar-width: 86px; }
    .mode-sidebar { padding: 13px 8px; }
    .brand { justify-content: center; padding: 0; }
    .brand-copy, .nav-label, .mode-button > span:not(.mode-icon), .mode-check, .sidebar-settings span, .version-label { display: none; }
    .mode-navigation { margin-top: 20px; }
    .mode-button { min-height: 58px; display: flex; justify-content: center; padding: 6px; }
    .mode-icon { width: 42px; height: 42px; }
    .sidebar-settings { justify-content: center; padding: 0; }
    .topbar { align-items: flex-start; flex-direction: column; padding: 12px 14px; }
    .topbar-status { width: 100%; justify-content: space-between; }
    .content-layout { padding: 12px; }
}

@media (max-width: 560px) {
    :root { --sidebar-width: 0px; }
    body { padding-bottom: 74px; font-size: 16px; }
    .mode-sidebar { inset: auto 0 0; width: 100%; height: 74px; display: flex; flex-direction: row; gap: 7px; padding: 7px 10px env(safe-area-inset-bottom, 7px); }
    .brand { display: none; }
    .mode-navigation { flex: 1; grid-template-columns: 1fr 1fr; gap: 7px; margin: 0; }
    .mode-button { min-height: 58px; flex-direction: row; gap: 8px; }
    .mode-button > span:not(.mode-icon) { display: block; }
    .mode-button strong { font-size: 12px; }
    .mode-button small { font-size: 9px; }
    .mode-icon { width: 36px; height: 36px; }
    .sidebar-footer { width: 58px; display: block; margin: 0; }
    .sidebar-settings { width: 58px; min-height: 58px; }
    .workspace { margin-left: 0; }
    .page-title-row { align-items: flex-start; flex-direction: column; gap: 5px; }
    .topbar h1 { font-size: 18px; }
    .network-state { display: none; }
    .content-layout { gap: 12px; }
    .stage-view { aspect-ratio: 4 / 3; }
    .stage-view > img { object-fit: cover; }
    .subtitle { bottom: 10px; width: calc(100% - 20px); font-size: 12px; }
    .stage-controls { flex-wrap: wrap; }
    .connection-controls { order: -1; width: 100%; }
    .connection-controls .button { flex: 1; }
    .control-group { width: calc(50% - 6px); }
    .control-group-end { margin-left: auto; }
    .voice-session { padding: 13px; }
    .number-grid { grid-template-columns: 1fr; }
    .settings-drawer { padding: 16px; }
    .toast-region { right: 12px; bottom: 86px; left: 12px; }
    .toast { min-width: 0; width: 100%; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}
