:root {
  --bg: #f3f4f1;
  --surface: #ffffff;
  --surface-alt: #f8f8f6;
  --line: #d9dcd6;
  --line-strong: #b9beb6;
  --text: #202421;
  --muted: #697069;
  --accent: #d44b36;
  --accent-hover: #b93d2c;
  --green: #217650;
  --green-bg: #e8f4ed;
  --amber: #9a6209;
  --amber-bg: #fff4d9;
  --red-bg: #fae9e5;
  --blue: #24679d;
  --shadow: 0 12px 36px rgb(33 38 34 / 12%);
  font-family: Inter, "Segoe UI", "Microsoft YaHei", sans-serif;
  color: var(--text);
  background: var(--bg);
}

* { box-sizing: border-box; }
html, body { min-height: 100%; margin: 0; }
body { min-width: 320px; font-size: 14px; letter-spacing: 0; }
button, input, select, textarea { font: inherit; letter-spacing: 0; }
button { cursor: pointer; }
.hidden { display: none !important; }

.auth-shell { min-height: 100vh; display: grid; place-items: center; padding: 24px; background: #edefeb; }
.auth-panel { width: min(420px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: 8px; box-shadow: var(--shadow); overflow: hidden; }
.auth-brand { padding: 24px 28px; border-bottom: 1px solid var(--line); background: var(--surface-alt); }
.auth-form { display: grid; gap: 18px; padding: 28px; }
.auth-form h1 { margin: 0 0 6px; font-size: 24px; }
.auth-form p { margin: 0; color: var(--muted); }
.brand { display: flex; align-items: center; gap: 11px; min-width: 0; }
.brand-mark { width: 34px; height: 34px; display: grid; place-items: center; flex: 0 0 auto; border-radius: 7px; background: var(--accent); color: #fff; font-weight: 800; }
.brand strong, .brand small { display: block; white-space: nowrap; }
.brand strong { font-size: 15px; }
.brand small { margin-top: 1px; color: var(--muted); font-size: 10px; text-transform: uppercase; }

label { display: grid; gap: 7px; color: #4e554f; font-size: 12px; font-weight: 650; }
input, select, textarea { width: 100%; border: 1px solid var(--line-strong); border-radius: 6px; background: #fff; color: var(--text); outline: none; }
input, select { min-height: 40px; padding: 0 11px; }
textarea { padding: 11px 12px; resize: vertical; line-height: 1.55; }
input:focus, select:focus, textarea:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgb(36 103 157 / 12%); }

.button { min-height: 38px; border: 1px solid transparent; border-radius: 6px; padding: 0 14px; display: inline-flex; align-items: center; justify-content: center; gap: 7px; font-weight: 700; }
.button.primary { background: var(--accent); color: white; }
.button.primary:hover { background: var(--accent-hover); }
.button.secondary { border-color: var(--line-strong); background: var(--text); color: white; }
.button.quiet { border-color: var(--line); background: var(--surface); color: var(--text); }
.button.quiet:hover { background: var(--surface-alt); }
.button.danger { border-color: #e1b5ad; background: #fff; color: #a82f20; }
.button.wide { width: 100%; }
.button:disabled { cursor: not-allowed; opacity: .55; }
.icon-button { width: 36px; height: 36px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); color: var(--muted); font-size: 22px; line-height: 1; }
.form-error { border: 1px solid #e2b8b0; border-radius: 6px; background: var(--red-bg); color: #8f2f21; padding: 10px 12px; }

.app-shell { min-height: 100vh; display: grid; grid-template-columns: 184px minmax(0, 1fr); grid-template-rows: 58px minmax(0, 1fr); }
.topbar { position: sticky; top: 0; z-index: 20; grid-column: 1 / -1; display: flex; align-items: center; justify-content: space-between; gap: 20px; padding: 0 20px; border-bottom: 1px solid var(--line); background: rgb(255 255 255 / 96%); }
.topbar-actions { display: flex; align-items: center; gap: 10px; min-width: 0; }
.current-user { color: var(--muted); max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.health-badge { display: inline-flex; align-items: center; gap: 7px; border: 1px solid var(--line); border-radius: 999px; background: var(--surface-alt); padding: 6px 10px; color: var(--muted); font-size: 12px; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--amber); }
.health-badge.ok .status-dot { background: var(--green); }
.health-badge.bad .status-dot { background: var(--accent); }

.sidebar { grid-row: 2; border-right: 1px solid var(--line); background: var(--surface); padding: 16px 10px; }
.sidebar nav { display: grid; gap: 5px; position: sticky; top: 74px; }
.nav-button { height: 42px; border: 0; border-radius: 6px; background: transparent; color: #525a53; display: flex; align-items: center; gap: 11px; padding: 0 13px; text-align: left; font-weight: 700; }
.nav-button span { width: 18px; color: #7d847d; font-size: 18px; text-align: center; }
.nav-button:hover { background: var(--surface-alt); }
.nav-button.active { background: var(--red-bg); color: #9d3022; }
.nav-button.active span { color: var(--accent); }

.workspace { min-width: 0; padding: 26px 30px 50px; }
.view { display: none; max-width: 1320px; margin: 0 auto; }
.view.active { display: block; }
.page-header { min-height: 54px; display: flex; align-items: flex-start; justify-content: space-between; gap: 20px; margin-bottom: 20px; }
.page-header h1 { margin: 0 0 5px; font-size: 23px; line-height: 1.2; }
.page-header p { margin: 0; color: var(--muted); }

.provider-strip { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); border: 1px solid var(--line); border-radius: 8px; overflow: hidden; margin-bottom: 18px; background: var(--surface); }
.provider-chip { min-width: 0; padding: 12px 14px; border-right: 1px solid var(--line); }
.provider-chip:last-child { border-right: 0; }
.provider-chip-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.provider-chip strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.provider-chip small { display: block; margin-top: 5px; color: var(--muted); }
.state-pill { display: inline-flex; align-items: center; border-radius: 999px; padding: 3px 7px; font-size: 10px; font-weight: 800; color: var(--green); background: var(--green-bg); }
.state-pill.warn { color: var(--amber); background: var(--amber-bg); }
.state-pill.bad { color: #9f3021; background: var(--red-bg); }

.create-layout { display: grid; grid-template-columns: minmax(360px, .85fr) minmax(440px, 1.15fr); gap: 18px; align-items: stretch; }
.compose-panel, .result-panel { min-height: 520px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.compose-panel { padding: 20px; display: flex; flex-direction: column; gap: 17px; }
.compose-panel textarea { min-height: 190px; flex: 1; }
.field-heading, .result-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.field-heading label { color: var(--text); font-size: 14px; }
.field-heading span, .result-heading span { color: var(--muted); font-size: 12px; }
.prompt-options { display: grid; gap: 11px; padding-top: 13px; border-top: 1px solid var(--line); }
.inline-toggle { display: flex; align-items: center; gap: 9px; width: fit-content; color: var(--text); font-weight: 700; }
.inline-toggle input { width: 17px; min-height: 17px; accent-color: var(--accent); }
.tag-picker { position: relative; min-width: 0; }
.tag-picker-heading { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 7px; }
.tag-picker-heading label { font-weight: 700; }
.tag-picker-heading span { color: var(--muted); font-size: 11px; }
.tag-picker input { width: 100%; }
.tag-suggestions { position: absolute; z-index: 15; top: calc(100% + 5px); left: 0; right: 0; max-height: 245px; overflow-y: auto; border: 1px solid var(--line-strong); border-radius: 6px; background: var(--surface); box-shadow: var(--shadow); }
.tag-suggestion { width: 100%; min-height: 40px; display: grid; grid-template-columns: 9px minmax(0, 1fr) auto; align-items: center; gap: 9px; padding: 7px 10px; border: 0; border-bottom: 1px solid var(--line); background: var(--surface); color: var(--text); text-align: left; }
.tag-suggestion:last-child { border-bottom: 0; }
.tag-suggestion:hover, .tag-suggestion.active { background: var(--surface-alt); }
.tag-dot { width: 7px; height: 7px; border-radius: 50%; background: #637169; }
.tag-dot.artist { background: #b64d82; }
.tag-dot.copyright { background: #8158aa; }
.tag-dot.character { background: #2c79a8; }
.tag-dot.meta { background: #a36824; }
.tag-dot.local { background: var(--green); }
.tag-suggestion strong { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; }
.tag-suggestion small { color: var(--muted); font-size: 10px; }
.tag-empty { padding: 13px; color: var(--muted); font-size: 12px; text-align: center; }
.result-tools { display: flex; align-items: center; justify-content: flex-end; gap: 9px; min-width: 0; }
.result-tools .button { min-height: 32px; padding: 0 10px; font-size: 12px; }
.icon-button.compact { width: 32px; height: 32px; min-height: 32px; font-size: 17px; }
.control-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.generate-button { height: 46px; font-size: 15px; }
.result-panel { display: flex; flex-direction: column; padding: 20px; }
.result-heading h2 { margin: 0; font-size: 15px; }
.result-gallery { flex: 1; min-height: 420px; margin-top: 15px; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; align-content: start; }
.result-gallery.empty { place-items: center; color: var(--muted); background: #f4f5f2; border: 1px dashed var(--line-strong); border-radius: 6px; }
.result-item { position: relative; min-width: 0; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; background: var(--surface-alt); aspect-ratio: 1; }
.result-item img { display: block; width: 100%; height: 100%; object-fit: contain; }
.result-item-actions { position: absolute; right: 8px; bottom: 8px; left: 8px; display: flex; justify-content: flex-end; gap: 6px; }
.result-item-actions a, .result-item-actions button { min-height: 32px; display: inline-flex; align-items: center; border: 1px solid var(--line); border-radius: 5px; background: rgb(255 255 255 / 94%); color: var(--text); padding: 0 9px; text-decoration: none; font-size: 12px; font-weight: 700; box-shadow: 0 2px 8px rgb(0 0 0 / 12%); }
.result-item-actions button { color: #a82f20; }
.generation-progress { flex: 1; min-height: 420px; display: grid; place-content: center; justify-items: center; gap: 9px; color: var(--muted); }
.generation-progress strong { color: var(--text); font-size: 15px; }
.generation-progress small { color: var(--muted); }
.spinner { width: 26px; height: 26px; border: 3px solid #e0e3de; border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.filterbar { display: grid; grid-template-columns: minmax(220px, 1fr) 140px 180px 160px auto; gap: 10px; align-items: end; padding: 14px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); margin-bottom: 14px; }
.history-list { display: grid; gap: 10px; }
.history-item { display: grid; grid-template-columns: 82px minmax(220px, 1fr) 150px 130px; gap: 14px; align-items: center; border-bottom: 1px solid var(--line); background: var(--surface); padding: 14px; }
.history-item:first-child { border-radius: 8px 8px 0 0; }
.history-item:last-child { border-bottom: 0; border-radius: 0 0 8px 8px; }
.history-thumb { width: 82px; height: 82px; border-radius: 5px; border: 1px solid var(--line); object-fit: cover; background: var(--surface-alt); }
.history-thumb.placeholder { display: grid; place-items: center; color: var(--muted); }
.history-prompt { min-width: 0; }
.history-prompt-head { display: flex; align-items: flex-start; gap: 8px; }
.history-prompt-head strong { min-width: 0; flex: 1; display: -webkit-box; overflow: hidden; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-height: 1.45; }
.history-prompt small, .history-meta small { display: block; margin-top: 7px; color: var(--muted); }
.history-attempts { margin-top: 8px; }
.history-downloads { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 8px; }
.history-downloads a, .history-downloads button { min-height: 25px; display: inline-flex; align-items: center; border: 0; background: transparent; padding: 0; color: var(--blue); font-size: 11px; font-weight: 700; text-decoration: none; }
.history-downloads button { color: #a82f20; }
.history-downloads a:hover { text-decoration: underline; }
.history-attempts summary { color: var(--blue); cursor: pointer; font-size: 12px; }
.attempt-line { margin-top: 5px; color: var(--muted); font-size: 11px; }
.empty-row { border: 1px dashed var(--line-strong); border-radius: 8px; padding: 50px 20px; text-align: center; color: var(--muted); background: var(--surface); }

.stat-row { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; margin-bottom: 18px; }
.public-address { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; padding: 12px 14px; border: 1px solid #b9d7c7; border-radius: 7px; background: var(--green-bg); }
.public-address div { min-width: 0; }
.public-address strong, .public-address a { display: block; }
.public-address a { margin-top: 4px; color: var(--blue); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stat-item { border: 1px solid var(--line); border-radius: 7px; background: var(--surface); padding: 14px; }
.stat-item span { display: block; color: var(--muted); font-size: 11px; }
.stat-item strong { display: block; margin-top: 5px; font-size: 23px; }
.provider-list { display: grid; gap: 10px; }
.provider-row { display: grid; grid-template-columns: minmax(180px, 1.2fr) 140px 110px 145px auto; gap: 14px; align-items: center; padding: 15px; border: 1px solid var(--line); border-radius: 7px; background: var(--surface); }
.provider-name { min-width: 0; }
.provider-name strong, .provider-name small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.provider-name small, .provider-metric small { color: var(--muted); margin-top: 4px; }
.provider-metric strong, .provider-metric small { display: block; }
.row-actions { display: flex; justify-content: flex-end; gap: 6px; flex-wrap: wrap; }
.row-actions .button { min-height: 34px; padding: 0 10px; font-size: 12px; }

.table-wrap { border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: var(--surface); }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 13px 14px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
th { background: var(--surface-alt); color: var(--muted); font-size: 11px; text-transform: uppercase; }
tr:last-child td { border-bottom: 0; }

.modal { width: min(760px, calc(100vw - 32px)); max-height: calc(100vh - 32px); padding: 0; border: 1px solid var(--line-strong); border-radius: 8px; box-shadow: var(--shadow); color: var(--text); }
.modal.small { width: min(450px, calc(100vw - 32px)); }
.modal::backdrop { background: rgb(20 24 21 / 45%); }
.modal form { display: flex; flex-direction: column; max-height: calc(100vh - 34px); }
.modal-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 14px; padding: 17px 20px; border-bottom: 1px solid var(--line); }
.modal-header h2 { margin: 0 0 4px; font-size: 18px; }
.modal-header p { margin: 0; color: var(--muted); font-size: 12px; }
.modal-body { overflow-y: auto; padding: 18px 20px; }
.modal-footer { display: flex; justify-content: flex-end; gap: 8px; padding: 13px 20px; border-top: 1px solid var(--line); background: var(--surface-alt); }
.form-section + .form-section { margin-top: 22px; padding-top: 18px; border-top: 1px solid var(--line); }
.form-section h3 { margin: 0 0 13px; font-size: 13px; }
.form-grid { display: grid; gap: 13px; }
.form-grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.form-grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.span-2 { grid-column: span 2; }
.toggle-label { display: flex; align-items: center; gap: 9px; min-height: 40px; padding-top: 21px; }
.toggle-label input { width: 18px; min-height: 18px; accent-color: var(--accent); }
.advanced-settings { margin-top: 22px; padding-top: 14px; border-top: 1px solid var(--line); }
.advanced-settings summary { cursor: pointer; font-weight: 750; }
.advanced-settings[open] summary { margin-bottom: 14px; }

.toast-region { position: fixed; z-index: 100; right: 18px; top: 18px; width: min(360px, calc(100vw - 36px)); display: grid; gap: 8px; }
.toast { border: 1px solid var(--line-strong); border-left: 4px solid var(--green); border-radius: 6px; background: var(--surface); box-shadow: var(--shadow); padding: 12px 14px; }
.toast.error { border-left-color: var(--accent); }

@media (max-width: 980px) {
  .app-shell { grid-template-columns: 70px minmax(0, 1fr); }
  .sidebar { padding-inline: 8px; }
  .nav-button { justify-content: center; padding: 0; font-size: 0; }
  .nav-button span { font-size: 19px; }
  .create-layout { grid-template-columns: 1fr; }
  .compose-panel, .result-panel { min-height: 460px; }
  .filterbar { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .search-field { grid-column: span 2; }
  .stat-row { grid-template-columns: repeat(3, 1fr); }
  .provider-row { grid-template-columns: minmax(160px, 1fr) 120px 110px auto; }
  .provider-row .provider-model { display: none; }
}

@media (max-width: 680px) {
  .app-shell { display: block; padding-bottom: 64px; }
  .topbar { height: 56px; padding: 0 12px; }
  .topbar .brand small, .current-user, .health-badge span:last-child { display: none; }
  .topbar-actions { gap: 7px; }
  .sidebar { position: fixed; z-index: 30; left: 0; right: 0; bottom: 0; height: 62px; padding: 6px 8px; border: 0; border-top: 1px solid var(--line); }
  .sidebar nav { position: static; height: 100%; display: flex; justify-content: space-around; gap: 3px; }
  .nav-button { width: 25%; height: 48px; display: grid; place-items: center; gap: 0; font-size: 10px; padding: 3px; }
  .nav-button span { display: block; font-size: 18px; line-height: 18px; }
  .workspace { padding: 18px 12px 26px; }
  .page-header { align-items: center; margin-bottom: 15px; }
  .page-header h1 { font-size: 20px; }
  .page-header p { display: none; }
  .provider-strip { display: flex; overflow-x: auto; }
  .provider-chip { min-width: 180px; }
  .control-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .compose-panel, .result-panel { min-height: 0; padding: 15px; }
  .compose-panel textarea { min-height: 190px; }
  .tag-suggestions { position: static; margin-top: 5px; max-height: 190px; box-shadow: none; }
  .result-gallery, .generation-progress { min-height: 330px; }
  .filterbar { display: grid; grid-template-columns: 1fr 1fr; }
  .history-item { grid-template-columns: 64px minmax(0, 1fr); }
  .history-thumb { width: 64px; height: 64px; }
  .history-meta, .history-state { grid-column: 2; }
  .stat-row { grid-template-columns: repeat(2, 1fr); }
  .public-address { align-items: flex-start; flex-direction: column; }
  .provider-row { grid-template-columns: minmax(0, 1fr) auto; }
  .provider-row .provider-model, .provider-row .provider-metric { display: none; }
  .row-actions { grid-column: 1 / -1; justify-content: flex-start; }
  .form-grid.two, .form-grid.three { grid-template-columns: 1fr; }
  .span-2 { grid-column: span 1; }
  .toggle-label { padding-top: 0; }
  .table-wrap { overflow-x: auto; }
  table { min-width: 620px; }
}
