:root {
  --ink: #202123;
  --muted: #6f6f73;
  --line: #e6e6e8;
  --paper: #ffffff;
  --canvas: #ffffff;
  --soft: #f5f5f5;
  --sidebar: #f7f7f8;
  --sidebar-hover: #ececee;
  --blue: #2563eb;
  --blue-dark: #1d4ed8;
  --green: #168c62;
  --rose: #c43c4e;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  overflow: hidden;
  color: var(--ink);
  background: var(--canvas);
  font-family: Inter, "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
}

button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; }
h1, h2, p { margin-top: 0; }
h1, h2 { letter-spacing: 0; }

.shell { min-height: 100dvh; }

.login-panel { display: grid; min-height: 100dvh; place-items: center; padding: 24px; background: #fbfbfc; }
.login-layout { display: grid; width: min(860px, 100%); grid-template-columns: minmax(250px, .9fr) minmax(320px, .82fr); gap: 48px; align-items: center; }
.login-brand { color: var(--muted); }
.login-brand .brand-logo { width: 72px; height: 72px; margin-bottom: 22px; border-radius: 17px; }
.login-brand h1 { margin: 0 0 11px; color: var(--ink); font-size: 34px; }
.login-brand > p:last-child { margin: 0; font-size: 15px; }
.login-card { display: grid; gap: 16px; padding: 26px; border: 1px solid var(--line); border-radius: 12px; background: var(--paper); box-shadow: 0 18px 44px rgba(0, 0, 0, .06); }
.shared-account-note { margin: -4px 0 0; color: var(--muted); font-size: 12px; line-height: 1.55; }

.workspace { display: grid; grid-template-columns: 268px minmax(0, 1fr); min-height: 100dvh; }
.app-sidebar { display: grid; grid-template-rows: auto auto auto auto minmax(0, 1fr) auto auto; gap: 10px; min-width: 0; height: 100dvh; padding: 13px 10px 12px; overflow: hidden; border-right: 1px solid var(--line); background: var(--sidebar); }
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; padding: 6px 8px 10px; }
.brand-logo { width: 38px; height: 38px; flex: 0 0 auto; border-radius: 10px; object-fit: cover; }
.eyebrow { margin: 0 0 1px; color: var(--muted); font-size: 10px; font-weight: 750; }
.brand h1 { margin: 0; font-size: 16px; }

.new-task-button, .mode-tab, .task-open, .task-delete, .icon-text-button, .small-button, .danger-button, .primary-button, .icon-button, .send-button { border: 0; }
.new-task-button { display: flex; align-items: center; gap: 10px; min-height: 42px; border: 1px solid #dadadc; border-radius: 9px; padding: 0 12px; color: var(--ink); background: var(--paper); font-weight: 700; text-align: left; }
.new-task-button span { font-size: 22px; font-weight: 400; line-height: 1; }
.new-task-button:hover { border-color: #c9c9cd; background: #fafafa; }
.sidebar-nav { display: grid; gap: 2px; }
.mode-tab { display: flex; align-items: center; gap: 11px; width: 100%; min-height: 38px; border-radius: 7px; padding: 0 10px; color: #4f4f53; background: transparent; font-size: 14px; text-align: left; }
.mode-tab span { width: 16px; color: var(--muted); font-size: 16px; text-align: center; }
.mode-tab:hover, .mode-tab.active { color: var(--ink); background: var(--sidebar-hover); font-weight: 700; }
.sidebar-label { margin: 12px 9px 2px; color: var(--muted); font-size: 11px; font-weight: 750; }

.task-list { display: grid; align-content: start; gap: 2px; min-height: 0; overflow-y: auto; padding-right: 1px; }
.task-row { display: grid; grid-template-columns: minmax(0, 1fr) 28px; align-items: center; min-height: 36px; border-radius: 7px; }
.task-row.active, .task-row:hover { background: var(--sidebar-hover); }
.task-open, .task-delete { min-width: 0; color: #4d4d51; background: transparent; }
.task-open { overflow: hidden; padding: 0 9px; text-align: left; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.task-row.active .task-open { color: var(--ink); font-weight: 700; }
.task-delete { display: grid; width: 28px; height: 28px; place-items: center; border-radius: 5px; opacity: 0; font-size: 18px; line-height: 1; }
.task-row:hover .task-delete, .task-row.active .task-delete { opacity: 1; }
.task-delete:hover { color: #fff; background: var(--rose); }

.account-strip { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1px; overflow: hidden; border: 1px solid var(--line); border-radius: 8px; background: var(--line); }
.account-stat { display: grid; gap: 3px; min-width: 0; min-height: 54px; padding: 8px; background: #fcfcfc; }
.account-stat span { overflow: hidden; color: var(--muted); font-size: 10px; font-weight: 700; text-overflow: ellipsis; white-space: nowrap; }
.account-stat strong { overflow: hidden; color: var(--ink); font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }
.sessionbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: 42px; padding: 5px 3px 0; border-top: 1px solid var(--line); }
.top-status { overflow: hidden; color: var(--muted); font-size: 12px; font-weight: 700; text-overflow: ellipsis; white-space: nowrap; }

.app-stage { min-width: 0; height: 100dvh; overflow-y: auto; background: var(--paper); }
.stage-header { position: sticky; top: 0; z-index: 4; display: flex; align-items: center; justify-content: space-between; min-height: 60px; padding: 0 26px; border-bottom: 1px solid var(--line); background: rgba(255, 255, 255, .94); backdrop-filter: blur(10px); }
.stage-heading { display: flex; align-items: center; gap: 10px; }
.stage-header h2 { margin: 0; font-size: 15px; font-weight: 700; }
.mobile-menu-button { display: none; }
.sidebar-backdrop { display: none; }
.icon-text-button { display: inline-flex; align-items: center; gap: 7px; min-height: 32px; border-radius: 7px; padding: 0 10px; color: var(--ink); background: transparent; font-size: 13px; font-weight: 700; }
.icon-text-button:hover { background: var(--soft); }
.icon-text-button span:first-child { font-size: 19px; font-weight: 400; line-height: 1; }

.chat-workspace { height: calc(100dvh - 60px); }
.chat-main { display: grid; grid-template-rows: minmax(0, 1fr) auto; min-width: 0; height: 100%; min-height: 0; overflow: hidden; }
.chat-thread { min-width: 0; min-height: 0; overflow-y: auto; padding: 36px clamp(22px, 8vw, 156px) 26px; scroll-behavior: smooth; }
.chat-empty { display: grid; min-height: 100%; place-content: center; padding-bottom: 11vh; color: var(--muted); text-align: center; }
.chat-empty h2 { margin: 0; color: var(--ink); font-size: clamp(25px, 3vw, 36px); }
.chat-empty p { margin: 10px 0 0; font-size: 15px; }
.chat-message { width: min(820px, 100%); margin: 0 auto 27px; }
.chat-message-heading { display: flex; align-items: center; justify-content: space-between; min-height: 22px; margin-bottom: 7px; }
.chat-role { color: var(--muted); font-size: 12px; font-weight: 750; }
.chat-copy-button { min-height: 26px; border: 1px solid transparent; border-radius: 6px; padding: 0 7px; color: var(--muted); background: transparent; font-size: 12px; }
.chat-copy-button:hover, .chat-copy-button:focus-visible { border-color: var(--line); color: var(--ink); background: var(--soft); }
.chat-message.user { padding-left: min(23vw, 220px); }
.chat-message.user .chat-message-heading { flex-direction: row-reverse; }
.chat-message.user .chat-role { color: #3c6dd8; }
.chat-content { white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.8; }
.chat-content code { border-radius: 5px; padding: 1px 5px; color: #a5283a; background: #f1f1f2; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .9em; }
.chat-message.user .chat-content { width: fit-content; max-width: 100%; margin-left: auto; border-radius: 18px 4px 18px 18px; padding: 10px 14px; color: #fff; background: #2e66e5; line-height: 1.55; }
.chat-message.pending .chat-content { opacity: .76; }
.chat-message.queued .chat-content { opacity: .52; }
.chat-message.assistant .chat-content { padding: 2px 0; }
.chat-message-image { display: block; width: min(340px, 100%); max-height: 360px; margin: 10px 0 0 auto; border: 1px solid var(--line); border-radius: 12px; object-fit: contain; background: #fff; }
.chat-image-note { display: inline-block; margin-top: 8px; color: var(--muted); font-size: 12px; }
.chat-waiting { width: min(820px, 100%); margin: 0 auto; color: var(--muted); font-size: 13px; }

.chat-composer { width: min(860px, calc(100% - 32px)); margin: 0 auto 22px; border: 1px solid #dedee0; border-radius: 21px; padding: 9px 11px 10px; background: var(--paper); box-shadow: 0 9px 28px rgba(0, 0, 0, .08); }
.chat-composer.dragging { border-color: var(--blue); background: #f5f8ff; box-shadow: 0 0 0 3px rgba(37, 99, 235, .12); }
.chat-attachment { display: inline-flex; align-items: center; gap: 9px; max-width: 100%; margin: 0 0 9px; border: 1px solid var(--line); border-radius: 8px; padding: 5px; color: var(--muted); font-size: 12px; }
.chat-attachment img { width: 42px; height: 42px; border-radius: 6px; object-fit: cover; }
.chat-attachment span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.composer-row { display: flex; align-items: end; gap: 8px; }
.icon-button, .send-button { display: inline-grid; width: 36px; height: 36px; flex: 0 0 auto; place-items: center; border-radius: 50%; font-size: 20px; line-height: 1; }
.icon-button { color: #55555a; background: transparent; }
.icon-button:hover { color: var(--ink); background: var(--soft); }
.attach-button { position: relative; overflow: hidden; }
.attach-button input { position: absolute; inset: 0; width: 100%; opacity: 0; cursor: pointer; }
.composer-row textarea { min-height: 38px; max-height: 180px; flex: 1; resize: none; border: 0; padding: 8px 1px; outline: 0; color: var(--ink); background: transparent; line-height: 1.45; }
.composer-row textarea::placeholder { color: #99999d; }
.send-button { color: #fff; background: var(--ink); font-weight: 900; }
.send-button:hover { background: #000; }
.send-button:disabled { opacity: .5; }
.composer-options { display: flex; align-items: center; flex-wrap: wrap; gap: 7px; padding: 6px 3px 0 43px; }
.compact-field { display: flex; align-items: center; gap: 5px; color: var(--muted); font-size: 11px; }
.compact-field select { min-height: 26px; max-width: 185px; border: 0; border-radius: 5px; padding: 0 5px; color: #55555a; background: var(--soft); outline: 0; font-size: 11px; }
.composer-options .message { margin: 0 0 0 auto; }

.image-workspace { display: grid; grid-template-columns: minmax(0, 1fr) minmax(300px, 360px); gap: 22px; align-items: start; padding: 26px; background: #fbfbfc; }
.image-main { display: grid; gap: 22px; min-width: 0; }
.canvas-area { display: grid; grid-template-columns: minmax(0, .85fr) minmax(0, 1.15fr); gap: 16px; }
.image-pane, .control-pane, .admin-panel { border: 1px solid var(--line); border-radius: 10px; background: var(--paper); }
.image-pane { padding: 16px; }
.control-pane { position: sticky; top: 78px; display: grid; gap: 14px; padding: 18px; }
.pane-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; min-height: 32px; margin-bottom: 5px; }
.pane-head h2 { margin: 0; font-size: 15px; }
.result-head-actions { display: flex; align-items: center; justify-content: flex-end; flex-wrap: wrap; gap: 8px; }

.small-button, .danger-button { min-height: 32px; border-radius: 7px; padding: 0 10px; background: #fff; font-size: 12px; font-weight: 700; }
.small-button { border: 1px solid #dedee0; color: var(--ink); }
.small-button:hover:not(:disabled) { border-color: #c7c7cb; background: #fafafa; }
.small-button:disabled { opacity: .45; }
.danger-button { border: 1px solid #f2c0c6; color: var(--rose); }
.danger-button:hover { border-color: var(--rose); background: #fff7f7; }
.primary-button { min-height: 42px; border-radius: 8px; padding: 0 15px; color: #fff; background: var(--ink); font-weight: 800; }
.primary-button:hover:not(:disabled) { background: #000; }
.primary-button:disabled { opacity: .6; }
.pill { display: inline-flex; align-items: center; min-height: 24px; border: 1px solid #dedee0; border-radius: 999px; padding: 0 8px; color: var(--muted); background: #fff; font-size: 11px; font-weight: 700; white-space: nowrap; }
.pill.safe { border-color: #b9dece; color: #08734d; background: #effaf5; }
.pill.busy { border-color: #bfcefa; color: #2556bb; background: #f1f5ff; }
.pill.error { border-color: #efc0c6; color: var(--rose); background: #fff5f6; }

.dropzone { position: relative; display: grid; min-height: clamp(310px, 55vh, 690px); place-items: center; overflow: hidden; border: 1px dashed #b9b9bf; border-radius: 8px; background: #fcfcfc; }
.dropzone.dragging { border-color: var(--blue); background: #f5f8ff; }
.dropzone input { position: absolute; inset: 0; width: 100%; opacity: 0; cursor: pointer; }
.dropzone.has-image { touch-action: pan-y; }
.dropzone.has-image input, .dropzone.has-image #preview { pointer-events: none; }
.empty-upload { display: grid; gap: 7px; place-items: center; padding: 20px; color: var(--muted); text-align: center; }
.empty-upload strong { color: var(--ink); font-size: 16px; }
.empty-upload small { font-size: 12px; }
#preview { width: 100%; height: 100%; min-height: clamp(310px, 55vh, 690px); object-fit: contain; background: #fff; }
.file-meta { min-height: 18px; margin: 9px 0 0; color: var(--muted); font-size: 12px; overflow-wrap: anywhere; }
.result-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; min-height: clamp(368px, 61vh, 754px); }
.empty-result { display: grid; min-height: clamp(310px, 55vh, 690px); place-items: center; border: 1px solid #ebebed; border-radius: 8px; color: var(--muted); background: #fcfcfc; }
.result-item { display: grid; grid-template-rows: minmax(190px, 1fr) auto; min-height: 300px; overflow: hidden; border: 1px solid #ebebed; border-radius: 8px; background: #fff; }
.result-item img { width: 100%; height: 100%; object-fit: contain; background: #fafafa; }
.result-actions { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: 46px; padding: 7px; border-top: 1px solid #ebebed; }
.result-index { color: var(--muted); font-size: 12px; }
.download-link { display: inline-flex; min-height: 30px; align-items: center; border-radius: 6px; padding: 0 9px; color: #fff; background: var(--green); font-size: 12px; font-weight: 700; text-decoration: none; }
.usage-box { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; margin-top: 12px; }
.usage-item { display: grid; gap: 4px; min-height: 62px; border: 1px solid #ebebed; border-radius: 7px; padding: 9px; background: #fcfcfc; }
.usage-item span { color: var(--muted); font-size: 11px; font-weight: 700; }
.usage-item strong { overflow-wrap: anywhere; color: var(--ink); font-size: 15px; }
.image-history { min-width: 0; border-top: 1px solid var(--line); padding-top: 16px; }
.history-list { display: grid; gap: 8px; }
.history-empty { display: grid; min-height: 90px; place-items: center; border: 1px dashed #d8d8dc; border-radius: 8px; color: var(--muted); background: #fff; font-size: 13px; }
.history-item { display: grid; grid-template-columns: 76px minmax(0, 1fr) auto; align-items: center; gap: 12px; min-width: 0; border: 1px solid var(--line); border-radius: 8px; padding: 8px; background: #fff; }
.history-preview { width: 76px; height: 76px; border-radius: 6px; object-fit: contain; background: #f7f7f8; }
.history-body { display: grid; gap: 7px; min-width: 0; }
.history-prompt { display: -webkit-box; overflow: hidden; font-size: 13px; line-height: 1.45; overflow-wrap: anywhere; -webkit-box-orient: vertical; -webkit-line-clamp: 2; }
.history-meta { overflow: hidden; color: var(--muted); font-size: 11px; text-overflow: ellipsis; white-space: nowrap; }
.history-open { white-space: nowrap; }

.field { display: grid; gap: 6px; min-width: 0; }
.field span { color: #4e4e53; font-size: 12px; font-weight: 700; }
.field input, .field select, .field textarea, .inline-input { width: 100%; min-height: 39px; border: 1px solid #d7d7db; border-radius: 7px; padding: 0 10px; color: var(--ink); background: #fff; outline: none; }
.field textarea { min-height: 146px; padding: 10px; resize: vertical; line-height: 1.5; }
.field input:focus, .field select:focus, .field textarea:focus, .inline-input:focus { border-color: #8baaf5; box-shadow: 0 0 0 3px rgba(37, 99, 235, .1); }
.split { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px; }
.message { min-height: 19px; margin: 0; color: var(--muted); font-size: 12px; line-height: 1.45; overflow-wrap: anywhere; }
.message.error { color: var(--rose); }
.message.ok { color: var(--green); }

.admin-panel { display: grid; gap: 15px; margin: 0 26px 26px; padding: 18px; }
.provider-form { display: grid; gap: 12px; }
.provider-shared { display: grid; grid-template-columns: minmax(0, 1.3fr) minmax(0, .9fr); gap: 12px; }
.provider-model-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 12px; }
.provider-models { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px; min-width: 0; margin: 0; border: 1px solid var(--line); border-radius: 7px; padding: 12px; }
.provider-models legend { padding: 0 5px; color: var(--muted); font-size: 12px; font-weight: 700; }
.provider-actions { display: flex; align-items: center; justify-content: flex-end; gap: 10px; }
.admin-create { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 160px 100px; align-items: end; gap: 12px; }
.users-list { display: grid; gap: 8px; }
.user-row { display: grid; grid-template-columns: 120px 76px 62px minmax(180px, 1fr) minmax(140px, .8fr) auto auto auto; align-items: center; gap: 8px; min-width: 0; padding: 10px; border: 1px solid var(--line); border-radius: 7px; background: #fcfcfc; }
.user-name { overflow: hidden; font-weight: 800; text-overflow: ellipsis; white-space: nowrap; }
.user-meta { color: var(--muted); font-size: 12px; }
.user-usage { display: grid; gap: 4px; min-width: 0; color: var(--muted); font-size: 11px; }

@media (max-width: 1160px) {
  .image-workspace, .canvas-area, .provider-model-grid, .provider-shared, .provider-models, .admin-create, .user-row { grid-template-columns: 1fr; }
  .control-pane { position: static; }
  .provider-actions { justify-content: stretch; }
  .provider-actions button { flex: 1; }
}

@media (max-width: 760px) {
  body { overflow: auto; }
  .login-panel { padding: 20px; }
  .login-layout { grid-template-columns: 1fr; gap: 28px; }
  .login-brand { text-align: center; }
  .workspace { display: block; }
  .app-sidebar { position: fixed; z-index: 20; inset: 0 auto 0 0; grid-template-rows: auto auto auto auto minmax(0, 1fr) auto auto; width: min(86vw, 320px); height: 100dvh; max-height: none; padding: 13px 10px 12px; transform: translateX(-105%); transition: transform .2s ease; border-right: 1px solid var(--line); border-bottom: 0; box-shadow: 18px 0 42px rgba(0, 0, 0, .14); }
  body.sidebar-open { overflow: hidden; }
  body.sidebar-open .app-sidebar { transform: translateX(0); }
  .sidebar-backdrop { position: fixed; z-index: 15; inset: 0; width: 100%; height: 100%; border: 0; background: rgba(0, 0, 0, .34); }
  body.sidebar-open .sidebar-backdrop { display: block; }
  .brand { padding-bottom: 4px; }
  .sidebar-nav { grid-template-columns: 1fr; }
  .task-list { display: grid; min-height: 0; overflow-y: auto; }
  .task-row { min-width: 0; }
  .account-strip { grid-template-columns: repeat(2, minmax(0, 1fr)); overflow: hidden; }
  .app-stage { min-height: 100dvh; height: 100dvh; overflow: hidden; }
  .stage-header { position: sticky; min-height: 54px; padding: 0 15px; }
  .mobile-menu-button { display: grid; width: 32px; height: 32px; place-items: center; border: 0; border-radius: 7px; padding: 0; color: var(--ink); background: transparent; font-size: 21px; line-height: 1; }
  .mobile-menu-button:hover { background: var(--soft); }
  .chat-workspace { height: calc(100dvh - 54px); min-height: 0; }
  .chat-thread { padding: 26px 16px 20px; }
  .chat-message.user { padding-left: 25px; }
  .chat-composer { width: calc(100% - 18px); margin-bottom: 10px; border-radius: 17px; }
  .composer-options { padding-left: 1px; }
  .composer-options .message { width: 100%; margin-left: 0; }
  .image-workspace { height: calc(100dvh - 54px); min-height: 0; padding: 15px; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
  .admin-panel { margin: 0 15px 15px; }
  .split, .usage-box { grid-template-columns: 1fr; }
  .history-item { grid-template-columns: 64px minmax(0, 1fr); }
  .history-preview { width: 64px; height: 64px; }
  .history-open { grid-column: 1 / -1; width: 100%; }
}
