.scroll-dialog {
  --muted-ink: #63746d;
  --rule: rgb(58 80 72 / 22%);
  width: min(960px, calc(100% - 64px));
  max-width: none;
  max-height: calc(100svh - 40px);
  padding: 20px 12px;
  border: 0;
  margin: auto;
  overflow: auto;
  overscroll-behavior: contain;
  background: transparent;
  color: var(--ink);
  font-family: "Segoe UI", "Microsoft YaHei", sans-serif;
  scrollbar-width: thin;
  scrollbar-color: #839187 transparent;
}

.scroll-dialog::backdrop {
  background: rgb(19 42 43 / 48%);
  backdrop-filter: blur(5px);
}

.scroll-dialog [hidden] { display: none !important; }
.settings-tabs { display: flex; gap: 30px; padding-top: 12px; border-bottom: 1px solid var(--rule); }
.scroll-dialog .settings-tabs button { position: relative; min-height: 52px; padding: 8px 3px 13px; border: 0; background: transparent; color: var(--muted-ink); font: 400 25px/1.3 "CoverBrush", "KaiTi", serif; }
.settings-tabs .tab-latin { font: 500 18px/1.3 "Segoe UI", sans-serif; letter-spacing: .04em; }
.settings-tabs [aria-selected="true"] { color: var(--cinnabar) !important; }
.settings-tabs [aria-selected="true"]::after { content: ""; position: absolute; left: 1px; right: 1px; bottom: 0; height: 2px; background: linear-gradient(90deg, transparent, #9d4937b0 15%, #9d4937 48%, transparent); transform: rotate(-1deg); }
.sound-preferences { min-height: 346px; padding-top: 12px; }
.sound-setting { padding: 10px 0 14px; }
.sound-setting + .sound-setting { border-top: 1px solid var(--rule); }
.sound-heading, .volume-heading { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.sound-heading h3 { margin: 0; font: 400 29px/1.5 "CoverBrush", "KaiTi", serif; }
.sound-heading .ink-choice { font-size: 14px; }
.volume-heading { margin-top: 12px; color: var(--muted-ink); font-size: 14px; }
.volume-heading output { font-variant-numeric: tabular-nums; color: #405a4e; }
.scroll-dialog .ink-range { --volume: 48%; display: block; appearance: none; -webkit-appearance: none; width: 100%; height: 44px; margin: 0; background: transparent; cursor: pointer; touch-action: pan-y; }
.ink-range::-webkit-slider-runnable-track { height: 4px; border-radius: 50%; background: linear-gradient(90deg, #49695b var(--volume), #9ba49466 var(--volume)); }
.ink-range::-moz-range-track { height: 4px; border-radius: 50%; background: linear-gradient(90deg, #49695b var(--volume), #9ba49466 var(--volume)); }
.ink-range::-webkit-slider-thumb { -webkit-appearance: none; width: 26px; height: 26px; margin-top: -11px; border: 0; border-radius: 50%; background: url('./assets/icons/ink-ring.svg') center / contain no-repeat, radial-gradient(circle, #f3eddf 58%, transparent 60%); }
.ink-range::-moz-range-thumb { width: 26px; height: 26px; border: 0; border-radius: 50%; background: url('./assets/icons/ink-ring.svg') center / contain no-repeat, radial-gradient(circle, #f3eddf 58%, transparent 60%); }
.ink-range:disabled { cursor: default; opacity: .45; }
.sound-preview { display: flex; justify-content: flex-end; }
.tutorial-setting { display: flex; align-items: center; justify-content: space-between; gap: 18px; margin-top: 12px; padding: 15px 0 4px; border-top: 1px solid var(--rule); }
.tutorial-setting h3 { margin: 0; font: 400 22px/1.4 "CoverBrush", "KaiTi", serif; color: #405a4e; }
.tutorial-setting p { margin: 3px 0 0; color: var(--muted-ink); font-size: 12px; }
.tutorial-setting button { flex: none; padding: 8px 13px; border: 1px solid #728572; background: #e5e5cf; color: #405a4e; font: 400 17px/1.4 "CoverBrush", "KaiTi", serif; }
.tutorial-setting button:hover { border-color: var(--cinnabar); color: var(--cinnabar); }
.scroll-dialog .preview-effect { padding: 5px 8px; border: 0; background: transparent; color: #405a4e; font: 400 23px/1.4 "CoverBrush", "KaiTi", serif; }
.preview-effect span { margin-left: 8px; font: 20px/1 serif; }
.preview-effect:disabled { opacity: .45; cursor: default; }
#preferences-status { margin: 0; }
.music-toggle { position: absolute; z-index: 2; top: clamp(12px, 2.5vw, 32px); right: clamp(12px, 2.5vw, 32px); display: grid; place-items: center; width: 50px; height: 50px; padding: 12px; border: 0; border-radius: 50%; background: url('./assets/icons/ink-ring.svg') center / 48px 48px no-repeat, radial-gradient(circle, #f3eddeee 58%, transparent 72%); color: #49625a; cursor: pointer; touch-action: manipulation; transition: color var(--feedback), transform var(--feedback); }
.music-toggle svg { width: 27px; height: 27px; overflow: visible; }
.music-toggle ellipse { fill: currentColor; }
.music-note { fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.music-slash { fill: none; stroke: var(--cinnabar); stroke-width: 2.4; stroke-linecap: round; opacity: 0; }
.music-toggle[aria-pressed="true"] { color: #85897c; }
.music-toggle[aria-pressed="true"] .music-slash { opacity: 1; }
.music-toggle:focus-visible { outline: 2px solid var(--cinnabar); outline-offset: 4px; }
.music-toggle:active { transform: translateY(1px); }

.scroll-dialog[open] { animation: unroll 260ms ease-out both; }

.scroll-mount {
  position: relative;
  padding: 15px 22px;
  background-color: #516f68;
  background-image: repeating-linear-gradient(0deg, transparent 0 2px, rgb(255 255 255 / 4%) 2px 3px), repeating-linear-gradient(90deg, transparent 0 3px, rgb(23 51 47 / 7%) 3px 4px);
  box-shadow: 0 14px 55px rgb(13 28 27 / 28%);
}

.scroll-mount::before,
.scroll-mount::after {
  content: "";
  position: absolute;
  z-index: 2;
  left: -10px;
  right: -10px;
  height: 15px;
  border-inline: 8px solid #46534a;
  border-radius: 4px;
  background: linear-gradient(#a9a184, #ded1ad 35%, #c0ae88 60%, #8c8063);
  box-shadow: 0 2px 4px rgb(29 39 28 / 20%);
  pointer-events: none;
}

.scroll-mount::before { top: -7px; }
.scroll-mount::after { bottom: -7px; }

.scroll-paper {
  position: relative;
  padding: 30px 38px 26px;
  background-color: #f3eddf;
  background-image: radial-gradient(ellipse at 8% 8%, rgb(176 158 113 / 10%), transparent 46%), radial-gradient(ellipse at 94% 95%, rgb(121 151 132 / 14%), transparent 45%), repeating-linear-gradient(4deg, transparent 0 4px, rgb(135 119 77 / 2%) 4px 5px);
  box-shadow: inset 0 0 22px rgb(128 105 65 / 9%), 0 0 2px rgb(31 46 33 / 25%);
}

.scroll-header,
.scroll-heading,
.api-heading,
.field-heading,
.scroll-footer,
.footer-actions { display: flex; align-items: center; }

.scroll-header {
  justify-content: space-between;
  padding-bottom: 25px;
  border-bottom: 1px solid var(--rule);
}

.scroll-heading { gap: 16px; }

.seal {
  display: grid;
  place-content: center;
  flex: none;
  width: 29px;
  height: 42px;
  border: 1px solid #a64b3c;
  color: #a64b3c;
  font: 17px/1.05 "CoverBrush", serif;
  box-shadow: inset 0 0 0 2px #f2edde, inset 0 0 0 3px #a64b3c88;
  transform: rotate(-4deg);
}

.scroll-heading h2 {
  margin: 0;
  font-family: "CoverBrush", "KaiTi", serif;
  font-size: 44px;
  font-weight: 400;
  line-height: 1.2;
  letter-spacing: 0.1em;
  outline: none;
}

.scroll-dialog button,
.scroll-dialog input,
.scroll-dialog select { font: inherit; }

.scroll-dialog button {
  min-height: 44px;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: background var(--feedback), color var(--feedback), transform var(--feedback);
}

.scroll-dialog button:active:not(:disabled) { transform: translateY(1px); }

.close-scroll,
.cancel-button {
  padding: 9px 10px;
  border: 0;
  background: transparent;
  color: var(--muted-ink);
}

.close-scroll { display: flex; align-items: center; gap: 10px; font-size: 14px !important; }
.close-scroll span { font-size: 25px; font-weight: 300; }

.api-columns { display: grid; grid-template-columns: 1fr 1fr; padding-top: 28px; }
.api-section { min-width: 0; padding-right: 30px; }
.api-section + .api-section { padding-right: 0; padding-left: 30px; border-left: 1px solid var(--rule); }
.api-heading { justify-content: space-between; min-height: 44px; margin-bottom: 15px; gap: 8px; }
.api-heading h3 { display: flex; align-items: center; gap: 7px; margin: 0; white-space: nowrap; font: 400 28px "CoverBrush", "KaiTi", serif; }
.latin { font: 500 18px "Segoe UI", sans-serif; letter-spacing: 0.05em; }

.ink-choice {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  flex: none;
  min-height: 44px;
  color: #405a4e;
  cursor: pointer;
  user-select: none;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.ink-choice input {
  position: absolute;
  top: 50%;
  left: 0;
  width: 32px;
  height: 32px;
  margin: 0;
  opacity: 0;
  transform: translateY(-50%);
  cursor: pointer;
}

.ink-circle,
.ink-orbit {
  position: relative;
  display: block;
  flex: none;
  width: 32px;
  height: 32px;
  background: url("./assets/icons/ink-ring.svg") center / contain no-repeat;
  pointer-events: none;
}

.ink-circle::after {
  content: "";
  position: absolute;
  inset: -2px;
  background: url("./assets/icons/ink-tick.svg") center / contain no-repeat;
  opacity: 0;
  transform: rotate(-7deg) scale(.7);
  transition: opacity var(--feedback), transform var(--feedback);
}

.ink-choice input:checked + .ink-circle::after { opacity: 1; transform: rotate(-3deg) scale(1); }
.ink-choice input:focus-visible + .ink-circle { outline: 1px solid var(--cinnabar); outline-offset: 3px; border-radius: 50%; }
.sync-choice .choice-label { font: 400 21px "CoverBrush", "KaiTi", serif; }
.choice-latin { font: 500 14px "Segoe UI", sans-serif; }
.sync-choice input:checked ~ .choice-label { color: var(--cinnabar); }
.api-section[data-linked="true"] :is(select, .key-field) { background-color: rgb(231 232 217 / 30%); }
.api-section[data-linked="true"] :is(select, input):disabled { color: #53665a; -webkit-text-fill-color: #53665a; opacity: 1; }
.api-section[data-linked="true"] .reveal-key:disabled { color: #7b8175; cursor: default; }
.field { margin-top: 18px; }
.field > label,
.field-heading > label { display: block; font-size: 14px; line-height: 22px; color: #445e56; }
.field > label { margin-bottom: 8px; }
.field-heading { justify-content: space-between; min-height: 44px; margin-top: -10px; margin-bottom: 2px; }

.scroll-dialog select,
.key-field {
  width: 100%;
  min-width: 0;
  min-height: 46px;
  border: 1px solid rgb(75 95 80 / 32%);
  border-radius: 2px;
  background-color: rgb(255 253 246 / 50%);
  color: #304d45;
  transition: border-color var(--feedback), box-shadow var(--feedback);
}

.scroll-dialog select {
  padding: 10px 28px 10px 11px;
  font-size: 13px;
  text-overflow: ellipsis;
  cursor: pointer;
}

.scroll-dialog select:disabled { color: #7b8175; background-color: rgb(237 231 214 / 40%); cursor: default; opacity: 1; }
.key-field { display: flex; align-items: stretch; }
.key-field input { width: 100%; min-width: 0; padding: 10px 11px; border: 0; outline: none; background: transparent; color: var(--ink); font-size: 14px; }
.key-field input::placeholder { color: #838878; }
.key-field:focus-within { border-color: #597b6e; box-shadow: 0 0 0 2px rgb(71 105 87 / 12%); }
.key-field:has(input[aria-invalid="true"]) { border-color: var(--cinnabar); }
.reveal-key { flex: none; min-width: 52px; padding: 0 10px; background: transparent; border: 0; color: var(--muted-ink); font-size: 12px !important; }
.scroll-dialog .fetch-models { position: relative; isolation: isolate; display: flex; align-items: center; gap: 5px; padding: 3px 0 3px 8px; border: 0; background: transparent; color: #405a4e; font: 400 20px "CoverBrush", "KaiTi", serif; }
.fetch-models::before { content: ""; position: absolute; z-index: -1; inset: 4px -7px; background: radial-gradient(ellipse at 48% 58%, rgb(91 118 89 / 15%), rgb(97 119 91 / 5%) 50%, transparent 72%); border-radius: 46% 58% 49% 55%; opacity: .4; transition: opacity var(--feedback); }
.fetch-models::after { content: ""; position: absolute; left: 43px; right: 2px; bottom: 6px; height: 1px; background: linear-gradient(90deg, transparent, rgb(60 87 68 / 42%) 25%, rgb(60 87 68 / 12%) 76%, transparent); transform: rotate(-1deg); }
.fetch-models .ink-orbit { width: 30px; height: 30px; }
.fetch-models:disabled { color: #7a8276; cursor: default; }
.fetch-models:disabled .ink-orbit { opacity: .45; }
.fetch-models[aria-busy="true"] { cursor: wait; }
.fetch-models[aria-busy="true"] .ink-orbit { animation: ink-turn 1.7s linear infinite; opacity: .8; }
.api-status { min-height: 36px; margin: 8px 0 0; font-size: 12px; line-height: 18px; color: var(--muted-ink); overflow-wrap: anywhere; }
.api-status[data-tone="error"],
.save-status[data-tone="error"] { color: #a14031; }

.scroll-footer { justify-content: space-between; gap: 16px; margin-top: 12px; padding-top: 18px; border-top: 1px solid var(--rule); }
.remember-key { color: #536859; font-size: 13px; }
.save-status { min-height: 18px; margin: 0 0 0 39px; font-size: 12px; line-height: 18px; color: var(--muted-ink); }
.footer-actions { gap: 12px; flex: none; }
.cancel-button { min-width: 62px; font-size: 14px !important; }
.save-button { display: flex; align-items: center; justify-content: center; gap: 24px; min-width: 128px; padding: 11px 22px; border: 1px solid #36574b; border-radius: 2px; background: #3f6456; box-shadow: inset 0 0 0 3px #3f6456, inset 0 0 0 4px rgb(239 234 214 / 26%); color: #f9f3e6; font-size: 16px !important; letter-spacing: 0.15em; }
.save-button:disabled { opacity: 0.55; cursor: wait; }

.scroll-dialog :is(button, select, input):focus-visible { outline: 2px solid var(--cinnabar); outline-offset: 3px; }
.scroll-dialog .key-field input:focus-visible { outline: none; }

@media (hover: hover) {
  .close-scroll:hover,
  .cancel-button:hover,
  .reveal-key:hover { color: var(--cinnabar); }
  .sync-choice:hover .choice-label,
  .fetch-models:hover:not(:disabled) { color: #752f23; }
  .fetch-models:hover:not(:disabled)::before { opacity: 1; }
  .save-button:hover { background: #2e5246; }
}

@keyframes unroll { from { opacity: 0; transform: translateY(10px) scaleY(0.98); } to { opacity: 1; transform: none; } }
@keyframes ink-turn { to { transform: rotate(360deg); } }

@media (min-width: 741px) and (max-height: 820px) {
  .scroll-paper { padding-top: 24px; padding-bottom: 20px; }
  .scroll-header { padding-bottom: 18px; }
  .api-columns { padding-top: 16px; }
  .field { margin-top: 14px; }
  .api-status { min-height: 28px; }
}

@media (max-width: 740px) {
  .settings-tabs { gap: 24px; padding-top: 7px; }
  .scroll-dialog .settings-tabs button { font-size: 23px; }
  .sound-heading h3 { font-size: 27px; }
  .sound-preferences { min-height: 318px; padding-top: 8px; }
  .scroll-dialog { width: calc(100% - 14px); max-height: calc(100svh - 18px); padding: 13px 9px; }
  .scroll-mount { padding: 11px 10px; }
  .scroll-mount::before,
  .scroll-mount::after { left: -5px; right: -5px; height: 12px; border-inline-width: 5px; }
  .scroll-mount::before { top: -5px; }
  .scroll-mount::after { bottom: -5px; }
  .scroll-paper { padding: 23px 20px 18px; }
  .scroll-header { padding-bottom: 20px; }
  .scroll-heading { gap: 10px; }
  .scroll-heading h2 { font-size: 34px; letter-spacing: 0.04em; }
  .seal { width: 23px; height: 33px; font-size: 14px; }
  .close-scroll { padding-inline: 4px; gap: 4px; }
  .api-columns { grid-template-columns: 1fr; padding-top: 17px; }
  .api-section { padding: 0; }
  .api-section + .api-section { margin-top: 5px; padding: 17px 0 0; border-left: 0; border-top: 1px solid var(--rule); }
  .api-heading { margin-bottom: 8px; }
  .api-heading h3 { font-size: 27px; }
  .field { margin-top: 12px; }
  .field > label { margin-bottom: 6px; }
  .scroll-dialog select { font-size: 14px; }
  .key-field input { font-size: 16px; }
  .api-status { min-height: 18px; }
  .scroll-footer { flex-direction: column; align-items: stretch; gap: 10px; margin-top: 18px; padding-top: 14px; }
  .footer-actions { justify-content: flex-end; }
  .save-button { min-width: 138px; }
  .save-status { margin-left: 39px; }
}

@media (max-width: 370px) {
  .scroll-paper { padding-inline: 14px; }
  .scroll-heading h2 { font-size: 30px; }
  .close-scroll { font-size: 12px !important; }
}

@media (prefers-reduced-motion: reduce) {
  .music-toggle { transition: none; }
  .music-toggle:active { transform: none; }
  .scroll-dialog[open],
  .fetch-models[aria-busy="true"] .ink-orbit { animation: none; }
  .scroll-dialog *,
  .scroll-dialog *::before,
  .scroll-dialog *::after { transition: none !important; }
  .scroll-dialog button:active:not(:disabled) { transform: none; }
}

/* Shared connection plus three model roles. The fixed line is shown once; each
   role keeps its own model list and fetch action. */
.api-settings-panel { display: block; padding-top: 20px; }
.shared-api-section { padding-bottom: 20px; border-bottom: 1px solid var(--rule); }
.shared-api-heading { align-items: baseline; margin-bottom: 12px; }
.shared-api-heading h3 { margin: 0; }
.shared-api-note, .model-role { color: var(--muted-ink); font-size: 12px; letter-spacing: .08em; }
.shared-fields { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 18px; }
.shared-fields .field { margin-top: 0; }
.fixed-url { width: 100%; min-height: 46px; padding: 10px 11px; border: 1px solid rgb(75 95 80 / 32%); border-radius: 2px; background: rgb(237 231 214 / 48%); color: #53665a; font: inherit; font-size: 13px; }
.fixed-url:focus-visible { outline: 2px solid var(--cinnabar); outline-offset: 3px; }
.model-slots { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; padding-top: 18px; }
.model-slot { min-width: 0; padding: 14px 14px 4px; border: 1px solid rgb(75 95 80 / 20%); border-radius: 2px; background: rgb(255 253 246 / 22%); }
.model-slot-heading { min-height: 52px; margin-bottom: 2px; }
.model-slot-heading h3 { margin: 0 0 3px; font-size: 25px; }
.model-slot .field { margin-top: 8px; }
.model-slot .field-heading { margin-top: 0; }
.model-slot .fetch-models { font-size: 17px; }
.model-slot .fetch-models .ink-orbit { width: 25px; height: 25px; }
.model-slot select { font-size: 13px; }
.image-model-slot { border-color: rgb(164 78 59 / 26%); }
.image-model-slot .model-role { color: #9b6858; }

@media (max-width: 740px) {
  .api-settings-panel { padding-top: 17px; }
  .shared-fields, .model-slots { grid-template-columns: 1fr; gap: 10px; }
  .shared-api-section { padding-bottom: 14px; }
  .shared-api-heading { margin-bottom: 8px; }
  .shared-api-note { font-size: 11px; }
  .model-slots { padding-top: 13px; }
  .model-slot { padding: 12px 12px 2px; }
  .model-slot-heading { min-height: 43px; }
  .model-slot-heading h3 { font-size: 24px; }
  .model-slot .api-status { min-height: 18px; }
}
