#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  padding-top: var(--safe-top);
}

#app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex: 0 0 auto;
  height: var(--header-height);
  padding: 0 var(--space-3);
  padding-left: calc(var(--space-3) + var(--safe-left));
  padding-right: calc(var(--space-3) + var(--safe-right));
  background: var(--bg);
  border-bottom: var(--border-hairline) solid var(--border);
  position: relative;
  z-index: var(--z-header);
}

#app-header-left,
#app-header-right {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-width: var(--tap-target);
  min-height: var(--tap-target);
}

#app-header-right {
  justify-content: flex-end;
}

#app-header-title {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  font-family: var(--font-display);
  font-size: var(--text-md);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-tight);
  color: var(--text);
  margin: 0;
  pointer-events: none;
  max-width: calc(100% - var(--header-title-reserve));
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#open-settings {
  width: var(--tap-target);
  height: var(--tap-target);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background var(--dur-fast) var(--ease-standard), color var(--dur-fast) var(--ease-standard);
}

#open-settings:hover {
  background: var(--surface);
  color: var(--text);
}

#open-settings:active {
  background: var(--surface-2);
}

#open-settings:focus-visible {
  outline: var(--focus-ring) solid var(--accent);
  outline-offset: var(--focus-ring);
}

#app-main {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding-bottom: calc(var(--tabbar-height) + var(--safe-bottom));
}

.tab {
  padding: var(--space-4);
  padding-left: calc(var(--space-4) + var(--safe-left));
  padding-right: calc(var(--space-4) + var(--safe-right));
  animation: tab-fade-in var(--dur-base) var(--ease-out);
}

@keyframes tab-fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

#app-tabbar {
  flex: 0 0 auto;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  background: var(--bg);
  border-top: var(--border-hairline) solid var(--border);
  padding-bottom: var(--safe-bottom);
  z-index: var(--z-tabbar);
}

.tab-button {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--tab-button-gap);
  height: var(--tabbar-height);
  padding: 0;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-family: inherit;
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: color var(--dur-fast) var(--ease-standard);
}

.tab-button svg {
  display: block;
  transition: color var(--dur-fast) var(--ease-standard);
}

.tab-button:hover {
  color: var(--text);
}

.tab-button:active {
  color: var(--text);
}

.tab-button[aria-selected="true"] {
  color: var(--accent);
}

.tab-button:focus-visible {
  outline: var(--focus-ring) solid var(--accent);
  outline-offset: calc(var(--focus-ring) * -1);
  border-radius: var(--radius-md);
}

.input,
.textarea {
  display: block;
  width: 100%;
  background: var(--surface);
  color: var(--text);
  border: var(--border-hairline) solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--space-3);
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: var(--leading-normal);
  transition: border-color var(--dur-fast) var(--ease-standard), background var(--dur-fast) var(--ease-standard);
  -webkit-appearance: none;
  appearance: none;
}

.textarea {
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  line-height: var(--leading-relaxed);
  resize: vertical;
  min-height: var(--textarea-min-height);
  tab-size: 2;
}

.input:focus,
.textarea:focus {
  outline: none;
  border-color: var(--accent);
  background: var(--surface-2);
}

.input::placeholder,
.textarea::placeholder {
  color: var(--text-tertiary);
}

.input:disabled,
.textarea:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.field-label {
  display: block;
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  color: var(--text-secondary);
  margin-bottom: var(--space-2);
}

.field {
  margin-bottom: var(--space-4);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: var(--tap-target);
  padding: 0 var(--space-4);
  font-family: inherit;
  font-size: var(--text-base);
  font-weight: var(--weight-semibold);
  border-radius: var(--radius-md);
  border: var(--border-hairline) solid transparent;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background var(--dur-fast) var(--ease-standard), border-color var(--dur-fast) var(--ease-standard), color var(--dur-fast) var(--ease-standard), opacity var(--dur-fast) var(--ease-standard);
  user-select: none;
}

.btn svg {
  display: block;
  flex: 0 0 auto;
}

.btn:hover {
  background: var(--surface-2);
}

.btn:active {
  background: var(--surface-3);
}

.btn:focus-visible {
  outline: var(--focus-ring) solid var(--accent);
  outline-offset: var(--focus-ring);
}

.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  pointer-events: none;
}

.btn-primary {
  background: var(--accent);
  color: var(--on-accent);
  border-color: var(--accent);
}

.btn-primary:hover {
  background: var(--accent);
  filter: brightness(1.08);
}

.btn-primary:active {
  background: var(--accent-pressed);
  border-color: var(--accent-pressed);
  filter: none;
}

.btn-secondary {
  background: transparent;
  color: var(--text);
  border-color: var(--border-strong);
}

.btn-secondary:hover {
  background: var(--surface);
  border-color: var(--text-tertiary);
}

.btn-secondary:active {
  background: var(--surface-2);
}

.btn-danger {
  background: transparent;
  color: var(--danger);
  border-color: var(--danger);
}

.btn-danger:hover {
  background: var(--danger-soft);
}

.btn-danger:active {
  background: var(--danger-soft-strong);
}

.btn-ghost {
  background: transparent;
  border-color: transparent;
  color: var(--text-secondary);
}

.btn-ghost:hover {
  background: var(--surface);
  color: var(--text);
}

.btn-ghost:active {
  background: var(--surface-2);
}

.btn-icon {
  width: var(--tap-target);
  min-width: var(--tap-target);
  padding: 0;
}

.btn-block {
  display: flex;
  width: 100%;
}

.card {
  background: var(--surface);
  border: var(--border-hairline) solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--space-4);
  margin-bottom: var(--space-4);
}

.card-title {
  font-size: var(--text-md);
  font-weight: var(--weight-semibold);
  color: var(--text);
  margin: 0 0 var(--space-3) 0;
}

.card-body {
  color: var(--text-secondary);
  font-size: var(--text-base);
  line-height: var(--leading-normal);
}

.banner {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-3);
  background: var(--surface);
  border: var(--border-hairline) solid var(--border);
  border-radius: var(--radius-md);
  margin-bottom: var(--space-4);
  position: relative;
  overflow: hidden;
  font-size: var(--text-sm);
  line-height: var(--leading-normal);
  color: var(--text-secondary);
}

.banner::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: var(--accent-rail);
  background: transparent;
}

.banner-icon {
  flex: 0 0 auto;
  width: var(--banner-icon-size);
  height: var(--banner-icon-size);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text-tertiary);
}

.banner-text {
  flex: 1 1 auto;
  min-width: 0;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.banner-ok {
  color: var(--text);
}

.banner-ok::before {
  background: var(--success);
}

.banner-ok .banner-icon {
  color: var(--success);
}

.banner-warn::before {
  background: var(--warning);
}

.banner-warn .banner-icon {
  color: var(--warning);
}

.banner-error {
  color: var(--text);
}

.banner-error::before {
  background: var(--danger);
}

.banner-error .banner-icon {
  color: var(--danger);
}

.banner-idle::before {
  background: var(--text-tertiary);
}

.row {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3);
  background: var(--surface);
  border: var(--border-hairline) solid var(--border);
  border-radius: var(--radius-md);
  margin-bottom: var(--space-2);
  min-height: var(--tap-target);
  transition: background var(--dur-fast) var(--ease-standard);
}

.row[role="button"],
.row.clickable {
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.row[role="button"]:hover,
.row.clickable:hover {
  background: var(--surface-2);
}

.row[role="button"]:active,
.row.clickable:active {
  background: var(--surface-3);
}

.row-main {
  flex: 1 1 auto;
  min-width: 0;
}

.row-side {
  flex: 0 0 auto;
  color: var(--text-tertiary);
  font-size: var(--text-sm);
  display: flex;
  align-items: center;
  gap: var(--space-1);
}

.story-row {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.story-row .story-title {
  font-size: var(--text-md);
  font-weight: var(--weight-medium);
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.story-row .story-meta {
  font-size: var(--text-xs);
  color: var(--text-tertiary);
  display: flex;
  gap: var(--space-2);
}

.story-row .story-badge {
  display: inline-block;
  padding: var(--border-hairline) var(--space-2);
  background: var(--surface-3);
  border-radius: var(--radius-sm);
  font-size: var(--text-xs);
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wide);
  font-weight: var(--weight-semibold);
}

.outline {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.outline-item {
  display: flex;
  flex-direction: column;
  gap: var(--border-hairline);
  padding: var(--space-2) var(--space-3);
  background: transparent;
  border: none;
  border-left: var(--focus-ring) solid transparent;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  text-align: left;
  cursor: pointer;
  color: var(--text-secondary);
  font-family: inherit;
  font-size: var(--text-sm);
  -webkit-tap-highlight-color: transparent;
  transition: background var(--dur-fast) var(--ease-standard), color var(--dur-fast) var(--ease-standard), border-color var(--dur-fast) var(--ease-standard);
}

.outline-item:hover {
  background: var(--surface);
  color: var(--text);
}

.outline-item.active {
  border-left-color: var(--accent);
  background: var(--surface);
  color: var(--text);
}

.outline-item-label {
  font-size: var(--text-xs);
  color: var(--text-tertiary);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wide);
  font-weight: var(--weight-semibold);
}

.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: var(--space-7) var(--space-4);
  color: var(--text-secondary);
}

.empty-title {
  font-size: var(--text-md);
  font-weight: var(--weight-semibold);
  color: var(--text);
  margin-bottom: var(--space-2);
}

.empty-body {
  font-size: var(--text-base);
  color: var(--text-secondary);
  margin-bottom: var(--space-5);
  max-width: 34ch;
  line-height: var(--leading-relaxed);
}

.empty-action {
  display: flex;
  gap: var(--space-2);
  flex-wrap: wrap;
  justify-content: center;
}

.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: var(--overlay);
  z-index: var(--z-overlay);
  opacity: 0;
  transition: opacity var(--dur-base) var(--ease-standard);
}

.sheet-backdrop.open {
  opacity: 1;
}

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--surface);
  border-top-left-radius: var(--radius-xl);
  border-top-right-radius: var(--radius-xl);
  z-index: var(--z-overlay);
  padding-bottom: var(--safe-bottom);
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  transform: translateY(100%);
  transition: transform var(--dur-base) var(--ease-out);
  box-shadow: var(--shadow-lg);
}

.sheet.open {
  transform: translateY(0);
}

.sheet-grabber {
  width: var(--grabber-width);
  height: var(--grabber-height);
  border-radius: var(--radius-pill);
  background: var(--border-strong);
  margin: var(--space-2) auto var(--space-3) auto;
  flex: 0 0 auto;
}

.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--space-4) var(--space-3) var(--space-4);
  border-bottom: var(--border-hairline) solid var(--border);
  flex: 0 0 auto;
}

.sheet-title {
  font-size: var(--text-md);
  font-weight: var(--weight-semibold);
  color: var(--text);
}

.sheet-body {
  padding: var(--space-4);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  flex: 1 1 auto;
  min-height: 0;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar-height) + var(--safe-bottom) + var(--space-3));
  transform: translate(-50%, var(--toast-offset));
  padding: var(--space-2) var(--space-4);
  background: var(--surface-3);
  color: var(--text);
  border: var(--border-hairline) solid var(--border-strong);
  border-radius: var(--radius-pill);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  box-shadow: var(--shadow-md);
  opacity: 0;
  transition: opacity var(--dur-base) var(--ease-standard), transform var(--dur-base) var(--ease-out);
  z-index: var(--z-toast);
  pointer-events: none;
  max-width: calc(100% - var(--space-6));
  text-align: center;
}

.toast.visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.toast-ok {
  border-color: var(--success);
}

.toast-warn {
  border-color: var(--warning);
}

.toast-error {
  border-color: var(--danger);
}

.dialog-backdrop {
  position: fixed;
  inset: 0;
  background: var(--overlay);
  z-index: var(--z-dialog);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
}

.dialog {
  background: var(--surface);
  border: var(--border-hairline) solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: var(--dialog-max-width);
  padding: var(--space-5);
}

.dialog-title {
  font-size: var(--text-md);
  font-weight: var(--weight-semibold);
  color: var(--text);
  margin: 0 0 var(--space-3) 0;
  text-align: center;
}

.dialog-body {
  color: var(--text-secondary);
  font-size: var(--text-sm);
  line-height: var(--leading-normal);
  margin-bottom: var(--space-5);
  text-align: center;
}

.dialog-actions {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.dialog-actions-row {
  display: flex;
  gap: var(--space-2);
}

.dialog-actions-row .btn {
  flex: 1 1 0;
}

.progress {
  height: var(--progress-height);
  background: var(--surface-3);
  border-radius: var(--radius-pill);
  overflow: hidden;
  margin-bottom: var(--space-2);
}

.progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: var(--radius-pill);
  width: 0%;
  transition: width var(--dur-base) var(--ease-standard);
}

.progress-text {
  font-size: var(--text-sm);
  color: var(--text-secondary);
  text-align: center;
}

.pill-group {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
}

.settings-range {
  display: block;
  width: 100%;
  height: var(--tap-target);
  background: transparent;
  -webkit-appearance: none;
  appearance: none;
  padding: 0;
  margin: 0;
  cursor: pointer;
}

.settings-range::-webkit-slider-runnable-track {
  height: 2px;
  background: var(--surface-3);
  border-radius: var(--radius-pill);
}

.settings-range::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: var(--space-5);
  height: var(--space-5);
  background: var(--on-accent);
  border: var(--border-hairline) solid var(--border-strong);
  border-radius: var(--radius-pill);
  margin-top: calc((2px - var(--space-5)) / 2);
  box-shadow: var(--shadow-sm);
}

.settings-range::-moz-range-track {
  height: 2px;
  background: var(--surface-3);
  border-radius: var(--radius-pill);
}

.settings-range::-moz-range-thumb {
  width: var(--space-5);
  height: var(--space-5);
  background: var(--on-accent);
  border: var(--border-hairline) solid var(--border-strong);
  border-radius: var(--radius-pill);
  box-shadow: var(--shadow-sm);
}

.settings-range:focus-visible {
  outline: var(--focus-ring) solid var(--accent);
  outline-offset: var(--focus-ring);
  border-radius: var(--radius-md);
}

.settings-footer {
  padding-top: var(--space-4);
  padding-bottom: var(--space-2);
  color: var(--text-tertiary);
  font-size: var(--text-xs);
  text-align: center;
}

.preview-shell {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.preview-frame {
  position: relative;
  width: 100%;
  background: var(--surface);
  border: var(--border-hairline) solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
  margin: 0 auto;
}

.preview-chat {
  position: absolute;
  inset: 0;
  overflow: hidden;
}

.preview-controls {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  background: var(--surface);
  border: var(--border-hairline) solid var(--border);
  border-radius: var(--radius-md);
}

.scrubber {
  flex: 1 1 auto;
  height: var(--tap-target);
  background: transparent;
  -webkit-appearance: none;
  appearance: none;
  padding: 0;
  margin: 0;
  cursor: pointer;
  min-width: 0;
}

.scrubber::-webkit-slider-runnable-track {
  height: 2px;
  background: var(--surface-3);
  border-radius: var(--radius-pill);
}

.scrubber::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: var(--space-4);
  height: var(--space-4);
  background: var(--accent);
  border: none;
  border-radius: var(--radius-pill);
  margin-top: calc((2px - var(--space-4)) / 2);
}

.scrubber::-moz-range-track {
  height: 2px;
  background: var(--surface-3);
  border-radius: var(--radius-pill);
}

.scrubber::-moz-range-thumb {
  width: var(--space-4);
  height: var(--space-4);
  background: var(--accent);
  border: none;
  border-radius: var(--radius-pill);
}

.scrubber:focus-visible {
  outline: var(--focus-ring) solid var(--accent);
  outline-offset: var(--focus-ring);
  border-radius: var(--radius-md);
}

.preview-time {
  font-size: var(--text-xs);
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  min-width: 8ch;
  text-align: right;
}
.hidden {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: var(--border-hairline);
  height: var(--border-hairline);
  padding: 0;
  margin: calc(var(--border-hairline) * -1);
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.spacer {
  flex: 1 1 auto;
}

.editor-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-bottom: var(--space-3);
}

.editor-outline {
  margin-top: var(--space-3);
}

.banner-link {
  display: block;
  margin-top: var(--space-2);
}

.empty-action-spaced {
  display: flex;
  justify-content: center;
  margin-top: var(--space-3);
}