:root {
  color-scheme: light;
  --bg: #f7f8fb;
  --panel: #ffffff;
  --panel-strong: #eef2f6;
  --text: #171a1f;
  --muted: #657083;
  --line: #d9e0ea;
  --accent: #0f766e;
  --accent-strong: #0b5f59;
  --warn: #b45309;
  --danger: #b91c1c;
  --blue: #2563eb;
  --shadow: 0 18px 50px rgba(22, 32, 45, 0.09);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    "Microsoft YaHei", sans-serif;
}

body.auth-required {
  overflow: hidden;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.58;
}

.app-header {
  align-items: center;
  background: #111827;
  color: #fff;
  display: flex;
  justify-content: space-between;
  min-height: 84px;
  padding: 18px clamp(16px, 3vw, 32px);
}

.app-chrome {
  min-height: 100vh;
  transition: filter 160ms ease;
}

.app-locked {
  filter: blur(16px) brightness(0.32);
  pointer-events: none;
  user-select: none;
}

.app-header h1 {
  font-size: clamp(22px, 3vw, 31px);
  line-height: 1.05;
  margin: 0 0 8px;
}

.app-header p {
  color: #b9c4d4;
  font-size: 14px;
  margin: 0;
}

.header-actions,
.form-actions,
.history-header,
.result-header,
.stream-row,
.mask-toolbar,
.image-actions {
  align-items: center;
  display: flex;
  gap: 10px;
}

.status-badge {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 999px;
  color: #e7edf6;
  font-size: 13px;
  padding: 7px 10px;
  white-space: nowrap;
}

.status-badge.ok {
  background: rgba(15, 118, 110, 0.22);
  border-color: rgba(45, 212, 191, 0.45);
}

.status-badge.warn {
  background: rgba(180, 83, 9, 0.2);
  border-color: rgba(251, 191, 36, 0.45);
}

.app-shell {
  display: grid;
  gap: 16px;
  grid-template-columns: minmax(330px, 420px) minmax(360px, 1fr) minmax(280px, 350px);
  padding: 16px;
}

.control-panel,
.result-panel,
.history-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.control-panel {
  align-self: start;
  padding: 14px;
}

.connection-panel {
  background: linear-gradient(180deg, #f8fbff 0%, #eef6ff 100%);
  border: 1px solid var(--line);
  border-radius: 10px;
  display: grid;
  gap: 12px;
  margin-bottom: 14px;
  padding: 12px;
}

.connection-head {
  align-items: flex-start;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.connection-head strong {
  color: #1f2937;
  font-size: 14px;
}

.connection-head span {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
}

.connection-grid {
  display: grid;
  gap: 12px;
}

.fixed-connection {
  align-items: center;
  background: rgba(255, 255, 255, 0.72);
  border: 1px solid #d8e4f2;
  border-radius: 8px;
  display: flex;
  gap: 10px;
  justify-content: space-between;
  padding: 10px 12px;
}

.fixed-connection span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
}

.fixed-connection code {
  color: #0f172a;
  font-size: 13px;
  word-break: break-all;
}

.result-panel {
  min-height: calc(100vh - 118px);
  padding: 18px;
}

.history-panel {
  align-self: start;
  max-height: calc(100vh - 118px);
  overflow: hidden;
  padding: 14px;
}

.tabs {
  background: var(--panel-strong);
  border-radius: 8px;
  display: grid;
  gap: 4px;
  grid-template-columns: repeat(4, 1fr);
  margin-bottom: 14px;
  padding: 4px;
}

.tab-button {
  background: transparent;
  border-radius: 6px;
  color: var(--muted);
  min-height: 36px;
  padding: 8px 10px;
}

.tab-button.active {
  background: #fff;
  color: var(--text);
  box-shadow: 0 1px 8px rgba(30, 41, 59, 0.12);
}

.field {
  display: grid;
  gap: 7px;
  min-width: 0;
}

.field label,
.stream-row label,
.mask-toolbar label {
  color: #394455;
  font-size: 13px;
  font-weight: 650;
}

.prompt-field {
  margin-bottom: 14px;
}

input,
select,
textarea {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 7px;
  color: var(--text);
  min-height: 38px;
  outline: none;
  padding: 9px 10px;
  width: 100%;
}

textarea {
  line-height: 1.5;
  resize: vertical;
}

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

.settings-grid {
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  margin-top: 14px;
}

.mode-panel {
  border-block: 1px solid var(--line);
  margin: 12px 0;
  padding: 12px 0;
}

.hidden {
  display: none !important;
}

.thumb-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.thumb {
  background: var(--panel-strong);
  border: 1px solid var(--line);
  border-radius: 7px;
  height: 74px;
  object-fit: cover;
  width: 74px;
}

.mask-tools {
  display: grid;
  gap: 10px;
  margin-top: 12px;
}

.mask-toolbar {
  flex-wrap: wrap;
}

#brushSize {
  max-width: 140px;
}

#maskCanvas {
  background:
    linear-gradient(45deg, #d9e0ea 25%, transparent 25%),
    linear-gradient(-45deg, #d9e0ea 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #d9e0ea 75%),
    linear-gradient(-45deg, transparent 75%, #d9e0ea 75%);
  background-color: #fff;
  background-position:
    0 0,
    0 8px,
    8px -8px,
    -8px 0;
  background-size: 16px 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: crosshair;
  display: block;
  max-height: 360px;
  max-width: 100%;
  width: auto;
}

.compact {
  margin-top: 2px;
}

.inline-check {
  align-items: center;
  display: inline-flex;
  gap: 8px;
}

.inline-check input {
  min-height: auto;
  width: auto;
}

.stream-row {
  background: #f5f7fa;
  border: 1px solid var(--line);
  border-radius: 8px;
  flex-wrap: wrap;
  justify-content: space-between;
  margin-top: 14px;
  padding: 10px;
}

#partials {
  max-width: 74px;
}

.advanced {
  border: 1px solid var(--line);
  border-radius: 8px;
  margin-top: 12px;
  padding: 10px;
}

.advanced summary {
  color: #394455;
  cursor: pointer;
  font-size: 13px;
  font-weight: 700;
}

.advanced textarea {
  margin-top: 10px;
}

.form-actions {
  justify-content: flex-end;
  margin-top: 14px;
}

.primary-button,
.ghost-button,
.image-actions button,
.history-item button {
  border-radius: 7px;
  min-height: 36px;
  padding: 8px 12px;
}

.primary-button {
  background: var(--accent);
  color: #fff;
  font-weight: 750;
}

.primary-button:hover {
  background: var(--accent-strong);
}

.header-ghost {
  background: rgba(255, 255, 255, 0.12);
  color: #e7edf6;
}

.header-ghost:hover {
  background: rgba(255, 255, 255, 0.2);
}

.ghost-button,
.image-actions button,
.history-item button {
  background: #eef2f6;
  color: #263244;
}

.ghost-button:hover,
.image-actions button:hover,
.history-item button:hover {
  background: #dfe6ef;
}

.result-header,
.history-header {
  justify-content: space-between;
  margin-bottom: 14px;
}

.result-header h2,
.history-header h2 {
  font-size: 20px;
  margin: 0 0 5px;
}

.result-header p {
  color: var(--muted);
  margin: 0;
}

.image-grid {
  display: grid;
  gap: 14px;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
}

.partial-grid {
  margin-bottom: 14px;
}

.image-card {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}

.image-card.partial {
  border-color: rgba(37, 99, 235, 0.45);
}

.image-card img {
  aspect-ratio: 1 / 1;
  background: #eef2f6;
  display: block;
  object-fit: contain;
  width: 100%;
}

.image-meta {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
  padding: 10px 10px 0;
}

.image-actions {
  justify-content: flex-end;
  padding: 10px;
}

.history-list {
  display: grid;
  gap: 10px;
  max-height: calc(100vh - 186px);
  overflow: auto;
  padding-right: 4px;
}

.history-item {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  display: grid;
  gap: 9px;
  padding: 10px;
}

.history-item img {
  aspect-ratio: 16 / 10;
  background: var(--panel-strong);
  border-radius: 7px;
  object-fit: cover;
  width: 100%;
}

.history-item strong {
  display: block;
  font-size: 14px;
  line-height: 1.35;
}

.history-item p {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
  margin: 0;
}

.history-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.empty-state {
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  padding: 18px;
  text-align: center;
}

.danger-text {
  color: var(--danger);
}

.auth-overlay {
  align-items: center;
  backdrop-filter: blur(14px);
  background: rgba(11, 15, 23, 0.82);
  display: flex;
  inset: 0;
  justify-content: center;
  padding: 20px;
  position: fixed;
  z-index: 50;
}

.auth-card {
  background: #fff;
  border: 1px solid rgba(217, 224, 234, 0.9);
  border-radius: 12px;
  box-shadow: 0 28px 80px rgba(15, 23, 42, 0.22);
  display: grid;
  gap: 16px;
  max-width: 460px;
  padding: 24px;
  width: min(100%, 460px);
}

.auth-copy h2 {
  font-size: 24px;
  margin: 0 0 8px;
}

.auth-copy p {
  color: var(--muted);
  line-height: 1.6;
  margin: 0;
}

.auth-copy code {
  color: #0f172a;
}

.auth-hint {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.6;
  margin: -4px 0 0;
}

.auth-hint a {
  color: var(--accent);
  word-break: break-all;
}

.auth-hint a:hover {
  color: var(--accent-strong);
}

.auth-actions {
  display: flex;
  justify-content: flex-end;
}

.auth-error {
  color: var(--danger);
  font-size: 13px;
  margin: -4px 0 0;
}

@media (max-width: 1220px) {
  .app-shell {
    grid-template-columns: minmax(320px, 410px) minmax(360px, 1fr);
  }

  .history-panel {
    grid-column: 1 / -1;
    max-height: none;
  }

  .history-list {
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    max-height: none;
  }
}

@media (max-width: 820px) {
  .app-header {
    align-items: flex-start;
    flex-direction: column;
    gap: 12px;
  }

  .app-shell {
    grid-template-columns: 1fr;
    padding: 10px;
  }

  .result-panel {
    min-height: auto;
  }

  .settings-grid {
    grid-template-columns: 1fr;
  }

  .tabs {
    grid-template-columns: repeat(2, 1fr);
  }
}
