:root {
  color-scheme: light dark;
  --bg: #f2f4f7;
  --surface: #ffffff;
  --surface-2: #f7f8fa;
  --text: #17202a;
  --muted: #69717c;
  --line: #e3e7eb;
  --accent: #5667e8;
  --accent-soft: #eef0ff;
  --success: #27855d;
  --success-soft: #e9f7f0;
  --warning: #a76b18;
  --warning-soft: #fff4dc;
  --danger: #b94c4c;
  --danger-soft: #fff0f0;
  --shadow: 0 8px 28px rgba(21, 28, 38, 0.07);
  --radius: 20px;
  font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101217;
    --surface: #191c23;
    --surface-2: #21252d;
    --text: #f3f5f7;
    --muted: #a5abb4;
    --line: #303640;
    --accent: #8895ff;
    --accent-soft: #242b4f;
    --success: #70d4a6;
    --success-soft: #17382b;
    --warning: #e4b15b;
    --warning-soft: #392d17;
    --danger: #f08888;
    --danger-soft: #3c2225;
    --shadow: 0 10px 32px rgba(0, 0, 0, 0.24);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--text); }
body { min-width: 280px; }
button { font: inherit; }

.app-shell { min-height: 100vh; min-height: 100dvh; padding-bottom: calc(84px + env(safe-area-inset-bottom)); }
.topbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: calc(16px + env(safe-area-inset-top)) 18px 13px; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(18px); border-bottom: 1px solid color-mix(in srgb, var(--line) 70%, transparent); }
.topbar h1 { margin: 1px 0 0; font-size: 21px; letter-spacing: -0.02em; }
.eyebrow { color: var(--muted); font-size: 11px; font-weight: 750; letter-spacing: .08em; text-transform: uppercase; }
.icon-button { width: 42px; height: 42px; border: 1px solid var(--line); border-radius: 14px; background: var(--surface); color: var(--text); cursor: pointer; font-size: 22px; box-shadow: 0 3px 12px rgba(0,0,0,.03); }
.icon-button:active, .primary-button:active, .nav-button:active, .list-card:active { transform: scale(.98); }

.main-content { width: min(760px, 100%); margin: 0 auto; padding: 16px 14px 24px; }
.hidden { display: none !important; }
.state-card, .welcome-card, .notice-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.state-card { display: flex; gap: 14px; align-items: center; padding: 18px; min-height: 100px; }
.state-card p, .welcome-card p, .notice-card p { margin: 6px 0 0; color: var(--muted); line-height: 1.45; font-size: 14px; }
.state-card.error { border-color: color-mix(in srgb, var(--danger) 35%, var(--line)); }
.state-icon { display: grid; place-items: center; width: 42px; height: 42px; flex: 0 0 42px; border-radius: 14px; background: var(--danger-soft); color: var(--danger); font-weight: 800; font-size: 20px; }
.spinner { width: 38px; height: 38px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.primary-button { margin-top: 13px; border: 0; border-radius: 12px; padding: 10px 15px; background: var(--accent); color: white; font-weight: 700; cursor: pointer; }

.welcome-card { display: flex; align-items: flex-start; justify-content: space-between; gap: 14px; padding: 18px; margin-bottom: 16px; background: linear-gradient(135deg, var(--surface), var(--accent-soft)); }
.welcome-card h2 { margin: 7px 0 0; font-size: 22px; letter-spacing: -.025em; }
.status-dot-row { color: var(--success); font-size: 12px; font-weight: 700; }
.status-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--success); margin-right: 5px; box-shadow: 0 0 0 4px color-mix(in srgb, var(--success) 12%, transparent); }
.phase-badge, .count-pill, .badge { white-space: nowrap; border-radius: 999px; font-size: 11px; font-weight: 750; }
.phase-badge { background: var(--surface); border: 1px solid var(--line); padding: 7px 10px; color: var(--muted); }

.metrics-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.metric-card { background: var(--surface); border: 1px solid var(--line); border-radius: 17px; padding: 14px; min-height: 105px; }
.metric-icon { font-size: 20px; margin-bottom: 13px; }
.metric-value { font-size: 27px; line-height: 1; font-weight: 800; letter-spacing: -.04em; }
.metric-label { color: var(--muted); font-size: 12px; margin-top: 7px; }

.section-heading { display: flex; align-items: end; justify-content: space-between; gap: 12px; margin: 25px 3px 10px; }
.section-heading.first { margin-top: 4px; }
.section-heading h3 { margin: 3px 0 0; font-size: 18px; letter-spacing: -.02em; }
.text-button { border: 0; background: transparent; color: var(--accent); font-weight: 750; cursor: pointer; padding: 8px; }
.count-pill { background: var(--surface-2); border: 1px solid var(--line); padding: 5px 9px; color: var(--muted); }

.stack-list { display: grid; gap: 9px; }
.list-card { width: 100%; text-align: left; display: flex; gap: 12px; align-items: center; border: 1px solid var(--line); border-radius: 16px; padding: 13px; background: var(--surface); color: var(--text); cursor: pointer; transition: transform .12s ease, border-color .12s ease; }
.list-card:hover { border-color: color-mix(in srgb, var(--accent) 35%, var(--line)); }
.list-icon { display: grid; place-items: center; flex: 0 0 42px; width: 42px; height: 42px; border-radius: 13px; background: var(--surface-2); font-size: 19px; }
.list-main { min-width: 0; flex: 1; }
.list-title { font-weight: 750; line-height: 1.25; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list-subtitle { display: flex; flex-wrap: wrap; gap: 6px 10px; color: var(--muted); font-size: 12px; margin-top: 5px; line-height: 1.35; }
.chevron { color: var(--muted); font-size: 22px; }
.badge { padding: 4px 7px; background: var(--surface-2); color: var(--muted); }
.badge.success { background: var(--success-soft); color: var(--success); }
.badge.warning { background: var(--warning-soft); color: var(--warning); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.empty-card { border: 1px dashed var(--line); border-radius: 16px; padding: 20px; text-align: center; color: var(--muted); background: color-mix(in srgb, var(--surface) 55%, transparent); font-size: 13px; }

.view { display: none; }
.view.active { display: block; }
.bottom-nav { position: fixed; left: 50%; bottom: calc(10px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 30; width: min(520px, calc(100% - 20px)); display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; padding: 7px; border-radius: 20px; background: color-mix(in srgb, var(--surface) 92%, transparent); border: 1px solid var(--line); box-shadow: 0 12px 40px rgba(0,0,0,.13); backdrop-filter: blur(20px); }
.nav-button { border: 0; border-radius: 14px; padding: 7px 3px 6px; background: transparent; color: var(--muted); cursor: pointer; }
.nav-button span { display: block; font-size: 19px; line-height: 1; }
.nav-button small { display: block; margin-top: 4px; font-size: 10px; }
.nav-button.active { background: var(--accent-soft); color: var(--accent); font-weight: 700; }

.system-grid { display: grid; gap: 10px; }
.system-card { background: var(--surface); border: 1px solid var(--line); border-radius: 17px; padding: 16px; }
.system-card-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.system-card h4 { margin: 0; font-size: 15px; }
.system-lines { display: grid; gap: 7px; margin-top: 12px; font-size: 13px; color: var(--muted); }
.system-line { display: flex; justify-content: space-between; gap: 12px; }
.system-line strong { color: var(--text); font-weight: 650; text-align: right; }
.notice-card { margin-top: 14px; padding: 16px; border-style: dashed; box-shadow: none; }

.modal-backdrop { position: fixed; inset: 0; z-index: 100; display: flex; align-items: flex-end; justify-content: center; padding-top: env(safe-area-inset-top); background: rgba(0,0,0,.38); backdrop-filter: blur(5px); }
.modal-sheet { width: min(760px, 100%); max-height: 88dvh; overflow: hidden; border-radius: 24px 24px 0 0; background: var(--surface); box-shadow: 0 -12px 50px rgba(0,0,0,.2); }
.modal-handle { width: 42px; height: 4px; border-radius: 999px; background: var(--line); margin: 9px auto 3px; }
.modal-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 16px 12px; border-bottom: 1px solid var(--line); }
.modal-header h3 { margin: 3px 0 0; font-size: 19px; }
.modal-content { padding: 16px; max-height: calc(88dvh - 77px); overflow: auto; padding-bottom: calc(24px + env(safe-area-inset-bottom)); }
.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }
.detail-box { border-radius: 14px; background: var(--surface-2); padding: 12px; }
.detail-box small { display: block; color: var(--muted); font-size: 10px; text-transform: uppercase; letter-spacing: .06em; }
.detail-box strong { display: block; margin-top: 5px; font-size: 13px; line-height: 1.3; }
.detail-section { margin-top: 17px; }
.detail-section h4 { margin: 0 0 8px; font-size: 13px; }
.message-preview { white-space: pre-wrap; word-break: break-word; line-height: 1.5; font-size: 14px; background: var(--surface-2); border-radius: 14px; padding: 14px; max-height: 280px; overflow: auto; }
.chip-row { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { border: 1px solid var(--line); background: var(--surface-2); border-radius: 999px; padding: 6px 9px; font-size: 11px; }
.link-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; border: 1px solid var(--line); border-radius: 12px; padding: 10px; margin-top: 7px; }
.link-row span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; }
.link-row button { border: 0; background: var(--accent-soft); color: var(--accent); border-radius: 9px; padding: 7px 9px; font-size: 11px; font-weight: 700; cursor: pointer; }

@media (min-width: 620px) {
  .metrics-grid { grid-template-columns: repeat(4, 1fr); }
  .system-grid { grid-template-columns: repeat(2, 1fr); }
}

/* v5 phase 2: editor */
.top-actions { display: flex; gap: 8px; align-items: center; }
.icon-button-primary { background: var(--accent); color: #fff; border-color: transparent; font-size: 26px; line-height: 1; }
.create-publication-card { width: 100%; display: flex; align-items: center; gap: 13px; margin: 0 0 14px; padding: 15px; border: 1px solid color-mix(in srgb, var(--accent) 35%, var(--line)); border-radius: 18px; background: linear-gradient(135deg, var(--accent-soft), var(--surface)); color: var(--text); text-align: left; cursor: pointer; box-shadow: var(--shadow); }
.create-publication-card > span:nth-child(2) { min-width: 0; flex: 1; }
.create-publication-card strong { display: block; font-size: 16px; }
.create-publication-card small { display: block; margin-top: 4px; color: var(--muted); font-size: 12px; line-height: 1.35; }
.create-publication-icon { display: grid; place-items: center; width: 42px; height: 42px; flex: 0 0 42px; border-radius: 14px; background: var(--accent); color: #fff; font-size: 25px; }

.editor-sheet { max-height: 94dvh; }
.editor-content { max-height: calc(94dvh - 77px); padding-bottom: 16px; }
.editor-section { margin-bottom: 19px; }
.editor-section:last-child { margin-bottom: 0; }
.editor-section-title { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 8px; }
.editor-section-title h4 { margin: 0; font-size: 14px; }
.editor-help { color: var(--muted); font-size: 11px; line-height: 1.4; }
.editor-label { display: block; margin-bottom: 7px; color: var(--muted); font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; }
.editor-input, .editor-select, .editor-textarea { width: 100%; border: 1px solid var(--line); border-radius: 13px; background: var(--surface-2); color: var(--text); outline: none; transition: border-color .12s ease, box-shadow .12s ease; }
.editor-input, .editor-select { min-height: 44px; padding: 10px 12px; }
.editor-textarea { min-height: 190px; resize: vertical; padding: 12px; line-height: 1.5; font: inherit; }
.editor-input:focus, .editor-select:focus, .editor-textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 13%, transparent); }
.editor-select option { color: initial; }
.field-meta { display: flex; justify-content: space-between; gap: 10px; margin-top: 6px; color: var(--muted); font-size: 11px; }
.editor-grid { display: grid; grid-template-columns: 1fr; gap: 10px; }
.editor-panel { border: 1px solid var(--line); border-radius: 15px; padding: 12px; background: color-mix(in srgb, var(--surface-2) 58%, var(--surface)); }
.editor-panel + .editor-panel { margin-top: 8px; }
.editor-row { display: flex; gap: 8px; align-items: center; }
.editor-row > *:first-child { flex: 1; min-width: 0; }
.small-button, .secondary-button, .danger-button { border: 1px solid var(--line); border-radius: 11px; padding: 9px 11px; background: var(--surface-2); color: var(--text); font-weight: 700; cursor: pointer; }
.small-button { padding: 7px 9px; font-size: 11px; }
.secondary-button { color: var(--accent); }
.danger-button { color: var(--danger); background: var(--danger-soft); border-color: color-mix(in srgb, var(--danger) 22%, var(--line)); }
.full-button { width: 100%; }
.editor-button-list { display: grid; gap: 8px; }
.editor-button-item { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: start; padding: 10px; border: 1px solid var(--line); border-radius: 13px; background: var(--surface-2); }
.editor-button-item strong { display: block; font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.editor-button-item small { display: block; color: var(--muted); font-size: 10px; margin-top: 3px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.library-add-row { display: grid; grid-template-columns: 1fr auto; gap: 8px; margin-top: 8px; }
.reaction-presets { display: flex; flex-wrap: wrap; gap: 7px; margin-bottom: 9px; }
.preset-button { border: 1px solid var(--line); border-radius: 999px; padding: 7px 10px; background: var(--surface-2); color: var(--text); cursor: pointer; font-size: 12px; }
.reaction-editor-row { display: grid; grid-template-columns: 1fr auto; gap: 8px; }
.media-drop { position: relative; border: 1px dashed var(--line); border-radius: 15px; padding: 18px 14px; text-align: center; background: color-mix(in srgb, var(--surface-2) 55%, transparent); }
.media-drop input { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer; }
.media-drop strong { display: block; font-size: 13px; }
.media-drop small { display: block; margin-top: 5px; color: var(--muted); font-size: 11px; }
.upload-list { display: grid; gap: 7px; margin-top: 9px; }
.upload-item { display: flex; align-items: center; gap: 9px; padding: 9px 10px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface-2); }
.upload-item-main { min-width: 0; flex: 1; }
.upload-item strong, .upload-item small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.upload-item strong { font-size: 12px; }
.upload-item small { margin-top: 3px; color: var(--muted); font-size: 10px; }
.upload-progress { display: flex; align-items: center; gap: 8px; margin-top: 8px; color: var(--muted); font-size: 11px; }
.upload-progress .spinner { width: 18px; height: 18px; border-width: 2px; }
.template-media-note { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px; margin-top: 8px; border-radius: 12px; background: var(--accent-soft); color: var(--accent); font-size: 11px; }
.delivery-switch { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.delivery-option { position: relative; display: block; }
.delivery-option input { position: absolute; opacity: 0; pointer-events: none; }
.delivery-option span { display: block; padding: 11px; border: 1px solid var(--line); border-radius: 13px; background: var(--surface-2); text-align: center; font-size: 12px; font-weight: 750; cursor: pointer; }
.delivery-option input:checked + span { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.schedule-field { margin-top: 10px; }
.editor-preview { border: 1px solid var(--line); border-radius: 16px; padding: 14px; background: var(--surface-2); }
.editor-preview-text { white-space: pre-wrap; word-break: break-word; line-height: 1.5; font-size: 13px; max-height: 240px; overflow: auto; }
.preview-meta { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }
.editor-footer { position: sticky; bottom: -16px; margin: 18px -16px -16px; padding: 12px 16px calc(14px + env(safe-area-inset-bottom)); display: grid; grid-template-columns: 1fr; gap: 8px; background: color-mix(in srgb, var(--surface) 94%, transparent); border-top: 1px solid var(--line); backdrop-filter: blur(18px); }
.editor-footer.two { grid-template-columns: 1fr 1.35fr; }
.editor-footer .primary-button { margin: 0; min-height: 44px; }
.editor-footer .secondary-button { min-height: 44px; }
.validation-error { padding: 10px 12px; margin-bottom: 12px; border-radius: 12px; background: var(--danger-soft); color: var(--danger); font-size: 12px; line-height: 1.4; }
.review-summary { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 14px; }
.review-summary .detail-box { min-height: 72px; }
.toast { position: fixed; left: 50%; bottom: calc(88px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 200; width: min(520px, calc(100% - 28px)); padding: 12px 14px; border-radius: 14px; background: #20242c; color: #fff; box-shadow: 0 12px 35px rgba(0,0,0,.28); font-size: 13px; font-weight: 650; text-align: center; }
.toast.success { background: #176845; }
.toast.error { background: #8b3037; }
button[disabled] { opacity: .55; cursor: wait; }

@media (min-width: 620px) {
  .editor-grid.two { grid-template-columns: 1fr 1fr; }
  .editor-sheet { border-radius: 24px 24px 0 0; }
}

/* v5.1.2 media preview */
.media-rule-help { margin-top: 7px; }
.media-preview-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 9px; margin-top: 10px; }
.media-preview-card { min-width: 0; overflow: hidden; border: 1px solid var(--line); border-radius: 14px; background: var(--surface-2); }
.media-preview-visual { min-height: 120px; display: flex; align-items: center; justify-content: center; background: color-mix(in srgb, var(--surface) 74%, #000 10%); overflow: hidden; }
.media-preview-image, .media-preview-video { display: block; width: 100%; height: 190px; object-fit: contain; background: #0b0d11; }
.media-preview-audio-icon, .media-preview-file-icon { display: flex; align-items: center; justify-content: center; min-height: 94px; width: 100%; font-size: 34px; }
.media-preview-audio { width: calc(100% - 18px); margin: 0 9px 10px; }
.media-preview-caption { min-width: 0; padding: 9px 10px 10px; }
.media-preview-caption strong, .media-preview-caption small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.media-preview-caption strong { font-size: 11px; }
.media-preview-caption small { margin-top: 3px; color: var(--muted); font-size: 9px; }
.editor-preview > .media-preview-grid { margin: 0 0 12px; }
.editor-preview-text.has-media { padding-top: 2px; }

@media (max-width: 430px) {
  .media-preview-grid { grid-template-columns: 1fr; }
  .media-preview-image, .media-preview-video { height: 210px; }
}

/* v5.2 scheduled management */
.detail-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }
.detail-actions .primary-button { margin-top: 0; }
.detail-actions button { min-height: 44px; }
@media (max-width: 460px) { .detail-actions { grid-template-columns: 1fr; } }
