:root {
    --bg: #f8f4ee;
    --surface: #fffdf9;
    --surface-strong: #ffffff;
    --text: #2d2926;
    --muted: #847a72;
    --border: #e7ddd3;
    --accent: #8b2f4b;
    --accent-soft: #f3e4e8;
    --shadow: 0 8px 24px rgba(64, 43, 34, 0.08);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    min-height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: "Noto Sans TC", "Outfit", sans-serif;
}

button, input, textarea { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
button:focus-visible, a:focus-visible, textarea:focus-visible {
    outline: 3px solid rgba(139, 47, 75, 0.42);
    outline-offset: 3px;
}

body { min-height: 100dvh; }

.app-shell {
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
}

.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: calc(12px + env(safe-area-inset-top, 0px)) 16px 10px;
    background: rgba(248, 244, 238, 0.9);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 20;
    backdrop-filter: blur(18px);
}

.app-header > div:first-child { min-width: 0; }

.eyebrow {
    margin: 0 0 3px;
    color: var(--accent);
    font-family: "Outfit", sans-serif;
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.12em;
}

h1 {
    margin: 0;
    max-width: min(210px, 100%);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 20px;
    font-weight: 600;
}

.header-actions { display: flex; align-items: center; gap: 8px; }
.save-status {
    max-width: 86px;
    overflow: hidden;
    color: var(--muted);
    font-size: 11px;
    line-height: 1.25;
    text-align: right;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.icon-btn {
    width: 44px;
    height: 44px;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: var(--surface);
    color: var(--text);
    display: inline-grid;
    place-items: center;
    text-decoration: none;
    font-size: 20px;
}
.icon-btn:disabled { opacity: 0.38; }
.icon-btn:active { transform: scale(0.97); }

.view-tabs {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 4px;
    margin: 12px 16px 8px;
    padding: 4px;
    border: 1px solid var(--border);
    border-radius: 16px;
    background: #eee7df;
}

.tab-btn {
    min-height: 44px;
    border: 0;
    border-radius: 12px;
    background: transparent;
    color: var(--muted);
    font-weight: 600;
}
.tab-btn.active { background: var(--surface-strong); color: var(--accent); box-shadow: 0 2px 8px rgba(64, 43, 34, 0.08); }

.main-content { flex: 1; min-height: 0; }
.view-panel { min-height: 100%; }
.outline-list { padding: 8px 16px calc(170px + var(--safe-bottom)); }

.outline-editor {
    padding: 8px 10px 0;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.outline-editor-header {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}

.outline-open-map {
    background: var(--surface);
    color: var(--text);
    border-color: var(--border);
}

.organizer-header { padding: 8px 10px 0; }

.organizer-header .outline-switch {
    border-color: transparent;
    background: transparent;
    color: var(--muted);
}

.outline-switch,
.outline-editor .outline-actions button,
.outline-apply-btn {
    min-width: 44px;
    min-height: 44px;
}

.outline-switch {
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 8px 14px;
    background: var(--surface);
    color: var(--text);
    font-weight: 600;
}

#outline-source {
    width: 100%;
    min-height: min(46vh, 320px);
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: 12px;
    resize: vertical;
    outline: none;
    background: #fffdfa;
    color: var(--text);
    font-size: 16px;
    line-height: 1.5;
    font-family: inherit;
}

#outline-source:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(139, 47, 75, 0.12);
}

.outline-error {
    margin: 0;
    min-height: 20px;
    color: #b42323;
    font-size: 14px;
}

.outline-actions {
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 8px;
}

.outline-actions button {
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--surface);
    color: var(--text);
    font-weight: 600;
    padding: 8px 4px;
}

.outline-actions .outline-apply-btn {
    grid-column: 5 / -1;
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}

@media (max-width: 340px) {
    .app-header {
        gap: 8px;
        padding-left: 10px;
        padding-right: 10px;
    }
    .header-actions { gap: 6px; }
    .desktop-link { display: none; }
}

@media (max-width: 430px) {
    .app-header { gap: 10px; }
    .header-actions { gap: 6px; }
    .save-status { max-width: 72px; }
    .icon-btn { width: 42px; height: 42px; border-radius: 13px; }
}

.outline-card {
    --depth: 0;
    --indent: clamp(0px, calc(var(--depth) * 18px), 72px);
    margin: 0;
    min-height: 44px;
    width: 100%;
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: center;
    gap: 10px;
    cursor: default;
    padding: 8px 10px;
    border: 0;
    border-bottom: 1px solid rgba(231, 221, 211, 0.86);
    border-radius: 0;
    background: var(--surface);
    box-shadow: none;
    position: relative;
}

.outline-card-dragging {
    opacity: 0.6;
    cursor: grabbing;
}

.outline-card-row-touch-ready {
    box-shadow: 0 0 0 1px rgba(45, 130, 242, 0.15);
}

.outline-marker-cell {
    position: relative;
    margin-left: var(--indent);
    min-height: 44px;
    width: calc(36px + var(--indent));
    display: grid;
    place-items: center;
    flex: 0 0 auto;
}

.outline-guides {
    position: absolute;
    inset: 0;
    pointer-events: none;
}

.outline-guide-line {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 1px;
    background: rgba(145, 155, 173, 0.5);
    z-index: 0;
}

.outline-marker-cell .collapse-toggle,
.outline-marker-cell .outline-leaf-marker {
    position: relative;
    z-index: 1;
}

.outline-drag-handle,
.outline-quick-move {
    min-height: 44px;
}

.outline-drag-handle {
    width: 44px;
    height: 44px;
    border-radius: 12px;
    display: grid;
    place-items: center;
    color: rgba(91, 80, 72, 0.62);
    font-size: 19px;
    font-weight: 700;
    background: transparent;
    border: 1px solid transparent;
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
}

.outline-drag-handle:active,
.outline-drag-handle:focus-visible {
    color: var(--text);
    border-color: rgba(139, 47, 75, 0.2);
    background: rgba(139, 47, 75, 0.06);
}

.outline-row-controls {
    display: none;
    align-items: center;
    gap: 6px;
}

.outline-card.selected .outline-row-controls,
.outline-card:focus-within .outline-row-controls {
    display: inline-flex;
}

.outline-content {
    min-width: 0;
    display: block;
}


.outline-card.drop-before,
.outline-card.drop-after {
    position: relative;
}

.outline-card.drop-before::before,
.outline-card.drop-after::after {
    content: '';
    position: absolute;
    left: 12px;
    right: 12px;
    height: 3px;
    border-radius: 999px;
    background: var(--accent);
}

.outline-card.drop-before::before {
    top: -2px;
}

.outline-card.drop-after::after {
    bottom: -2px;
}

.outline-card.drop-indent,
.outline-card.drop-outdent {
    position: relative;
    border-color: var(--accent);
    box-shadow: inset 0 0 0 2px rgba(139, 47, 75, 0.16), var(--shadow);
}

.outline-card.drop-indent::before,
.outline-card.drop-outdent::before {
    content: '';
    position: absolute;
    left: 8px;
    top: 50%;
    width: 18px;
    height: 18px;
    margin-top: -9px;
    border-radius: 50%;
    background: var(--accent-soft);
    color: var(--accent);
    display: grid;
    place-items: center;
    font-size: 11px;
    font-weight: 700;
}

.outline-card.drop-indent::before {
    content: '↳';
}

.outline-card.drop-outdent::before {
    content: '↰';
}

.outline-drag-hint {
    position: fixed;
    left: -9999px;
    top: -9999px;
    z-index: 55;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 7px 11px;
    border-radius: 999px;
    border: 1px solid var(--accent);
    background: rgba(255, 253, 249, 0.96);
    color: var(--text);
    font-size: 12px;
    line-height: 1.4;
    font-weight: 600;
    box-shadow: 0 8px 24px rgba(70, 52, 44, 0.18);
    white-space: nowrap;
    pointer-events: none;
    opacity: 0;
    transform: translate(-50%, -6px) scale(0.94);
    transition: opacity 120ms ease, transform 120ms ease;
}

.outline-drag-hint.is-visible {
    opacity: 1;
    transform: translate(-50%, -6px) scale(1);
}

.outline-drag-hint::before {
    content: '';
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--accent);
}

.outline-card[data-drop-label]::after {
    content: attr(data-drop-label);
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    z-index: 3;
    height: auto;
    min-height: 20px;
    padding: 4px 8px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-size: 11px;
    line-height: 1.2;
    font-weight: 600;
    white-space: nowrap;
    pointer-events: none;
    opacity: 0.96;
}

.outline-card.drop-before[data-drop-label]::after,
.outline-card.drop-after[data-drop-label]::after {
    left: 12px;
    transform: translateX(0);
    width: auto;
}

.outline-card.drop-before[data-drop-label]::after {
    bottom: calc(100% + 4px);
}

.outline-card.drop-after[data-drop-label]::after {
    top: calc(100% + 4px);
}

.outline-card.drop-indent[data-drop-label]::after,
.outline-card.drop-outdent[data-drop-label]::after {
    left: 32px;
    transform: translateX(0);
    top: 50%;
    transform: translate(0, -50%);
    background: rgba(251, 246, 249, 0.96);
}

.outline-drag-hint.is-indent::before,
.outline-drag-hint.is-outdent::before {
    content: '';
}

.outline-drag-hint.is-reorder::before {
    width: 0;
    height: 0;
}

.outline-drag-hint.is-indent { background: rgba(233, 245, 249, 0.96); }
.outline-drag-hint.is-outdent { background: rgba(255, 250, 243, 0.96); }

.outline-mini-btn {
    width: 44px;
    height: 44px;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--muted);
    font-size: 16px;
    line-height: 1;
    padding: 0;
    display: grid;
    place-items: center;
    min-width: 0;
}

.outline-mini-btn:disabled {
    opacity: 0.35;
}

.outline-card.root-card {
    background: transparent;
    border-color: var(--border);
    color: var(--text);
    font-weight: 700;
}

.node-topic {
    min-width: 0;
    flex: 1;
    font-size: 16px;
    font-weight: 500;
    overflow-wrap: anywhere;
    line-height: 1.45;
    text-align: left;
}

.node-topic[contenteditable="true"]:focus {
    cursor: text;
    outline: 2px solid rgba(139, 47, 75, 0.42);
    outline-offset: 2px;
    border-radius: 4px;
    background: transparent;
}

.root-card .node-topic {
    font-weight: 700;
}

.map-toolbar {
    display: grid;
    grid-template-columns: 46px 66px 46px 1fr;
    gap: 6px;
    padding: 6px 12px 0;
}
.map-toolbar button {
    min-height: 42px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: rgba(255, 253, 249, 0.78);
    color: var(--text);
    font-weight: 600;
    box-shadow: 0 2px 5px rgba(64, 43, 34, 0.04);
}
.map-toolbar button:first-child,
.map-toolbar button:nth-child(3) {
    color: var(--accent);
    font-size: 19px;
}
.map-toolbar button:nth-child(2) {
    background: var(--surface);
    color: var(--muted);
    font-size: 13px;
}
.map-toolbar button:last-child {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
    box-shadow: 0 4px 10px rgba(139, 47, 75, 0.18);
}
.map-gesture-hint {
    margin: 7px 14px 0;
    padding: 0 2px;
    color: var(--muted);
    font-size: 12px;
    line-height: 1.45;
}

.mobile-map {
    height: calc(100dvh - 202px - env(safe-area-inset-top, 0px));
    min-height: 420px;
    margin: 10px 12px calc(12px + var(--safe-bottom));
    overflow: hidden;
    touch-action: none;
    overscroll-behavior: contain;
    border: 1px solid var(--border);
    border-radius: 20px;
    background-color: var(--surface);
    background-image: radial-gradient(rgba(139, 47, 75, 0.1) 1px, transparent 1px);
    background-size: 18px 18px;
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.5), 0 6px 18px rgba(64, 43, 34, 0.05);
}

.mobile-map jmnode.map-dragging {
    z-index: 50 !important;
    opacity: 0.86;
    box-shadow: 0 12px 28px rgba(90, 37, 55, 0.28) !important;
    transition: none !important;
}
.mobile-map jmnode.map-drop-target {
    outline: 4px solid rgba(139, 47, 75, 0.42) !important;
    outline-offset: 5px;
}

.outline-quick-move {
    display: inline-flex;
    flex-direction: column;
    gap: 4px;
}

.outline-card.selected {
    border-radius: 14px;
    border: 1px solid rgba(139, 47, 75, 0.16);
    border-bottom-color: rgba(139, 47, 75, 0.16);
    background: rgba(243, 228, 232, 0.45);
    box-shadow: inset 3px 0 0 rgba(139, 47, 75, 0.45);
    z-index: 1;
}

.outline-card.selected .node-topic {
    color: var(--text);
}

.outline-quick-move {
    display: none;
}

.action-dock {
    position: fixed;
    left: 10px;
    right: 10px;
    bottom: calc(10px + var(--safe-bottom));
    z-index: 30;
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    gap: 3px;
    padding: 7px;
    border: 1px solid rgba(231, 221, 211, 0.85);
    border-radius: 14px;
    background: rgba(255, 253, 249, 0.92);
    box-shadow: 0 8px 22px rgba(50, 32, 25, 0.12);
    backdrop-filter: blur(20px);
}

.action-dock.is-map-mode {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.action-dock.is-map-mode > button:not(#action-add-child):not(#action-add-sibling) {
    display: none;
}

.action-dock button {
    min-width: 0;
    min-height: 50px;
    padding: 4px 1px;
    border: 0;
    border-radius: 12px;
    background: transparent;
    color: var(--muted);
    font-size: 10px;
}
.action-dock button span { display: block; color: var(--accent); font-size: 19px; line-height: 22px; }
.action-dock button:active { background: var(--accent-soft); }
.action-dock button:disabled,
.more-grid button:disabled {
    opacity: 0.38;
    cursor: not-allowed;
}

.mode-toggle-btn {
    display: none;
}

.sheet { position: fixed; inset: 0; z-index: 50; }
.sheet-backdrop { position: absolute; inset: 0; width: 100%; border: 0; background: rgba(36, 27, 23, 0.42); }
.sheet-panel {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    padding: 10px 18px calc(18px + var(--safe-bottom));
    border-radius: 24px 24px 0 0;
    background: var(--surface-strong);
    box-shadow: 0 -18px 50px rgba(40, 25, 20, 0.2);
}
.sheet-panel:focus { outline: none; }
.sheet-handle { width: 42px; height: 5px; margin: 0 auto 16px; border-radius: 999px; background: #d8cec5; }
.sheet h2 { margin: 0 0 16px; font-size: 20px; }
.file-subtitle {
    margin: 0 0 16px;
    color: var(--muted);
    font-size: 13px;
    line-height: 1.5;
}
.sheet label { display: block; margin-bottom: 7px; color: var(--muted); font-size: 13px; }
.sheet textarea {
    width: 100%;
    min-height: 104px;
    resize: none;
    padding: 13px;
    border: 1px solid var(--border);
    border-radius: 14px;
    outline: none;
    background: #fffdfa;
    color: var(--text);
    font-size: 17px;
}
.sheet textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(139, 47, 75, 0.12); }
.sheet-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 14px; }
.sheet-actions button { min-height: 48px; border-radius: 14px; font-weight: 600; }
.btn-secondary { border: 1px solid var(--border); background: var(--surface); color: var(--text); }
.btn-primary { border: 1px solid var(--accent); background: var(--accent); color: white; }

.compact-sheet { padding-bottom: calc(24px + var(--safe-bottom)); }
.more-grid,
.map-quick-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.more-grid button,
.map-quick-grid button {
    min-height: 82px;
    padding: 10px 5px;
    border: 1px solid var(--border);
    border-radius: 16px;
    background: var(--surface);
    color: var(--text);
    font-size: 13px;
}
.more-grid button span,
.map-quick-grid button span { display: block; margin-bottom: 5px; color: var(--accent); font-size: 24px; }
.more-grid .danger-action,
.map-quick-grid .danger-action { color: #a12828; border-color: #efcaca; background: #fff7f7; }
.more-grid .danger-action span,
.map-quick-grid .danger-action span { color: #b42323; }

.collapse-toggle {
    width: 18px;
    height: 18px;
    min-width: 18px;
    padding: 0;
    border: 0;
    border-radius: 0;
    background: transparent;
    color: var(--text);
    font-size: 14px;
    font-weight: 600;
    line-height: 1;
    text-align: center;
    user-select: none;
    display: grid;
    place-items: center;
}

.outline-leaf-marker {
    width: 7px;
    height: 7px;
    border-radius: 1px;
    background: #8c8c8c;
    border: 1px solid #6f6f6f;
    display: block;
}


.move-target-list {
    max-height: 60vh;
    overflow-y: auto;
    padding-bottom: calc(20px + var(--safe-bottom));
}
.move-target-item {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    min-height: 52px;
    padding: 10px 14px;
    margin-bottom: 6px;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: var(--surface);
    color: var(--text);
    font-size: 15px;
    text-align: left;
}
.move-target-item:active { background: var(--accent-soft); }
.move-target-item.disabled { opacity: 0.38; }
.move-target-item .move-depth { color: var(--muted); font-size: 11px; white-space: nowrap; }

body.has-selection .outline-list { padding-bottom: calc(190px + var(--safe-bottom)); }
body.has-selection .mobile-map { height: calc(100dvh - 300px - env(safe-area-inset-top, 0px)); }

@media (min-width: 1000px) {
    body.desktop-split-active .view-tabs { display: none; }
    body.desktop-split-active .main-content {
        display: grid;
        grid-template-columns: minmax(320px, 0.38fr) minmax(0, 0.62fr);
        gap: 12px;
        padding: 12px 16px 16px;
        overflow: hidden;
    }
    body.desktop-split-active .view-panel {
        min-width: 0;
        min-height: 0;
        overflow: hidden;
        border: 1px solid var(--border);
        border-radius: 18px;
        background: rgba(255, 253, 249, 0.72);
    }
    body.desktop-split-active #outline-panel { overflow: auto; }
    body.desktop-split-active #map-panel { display: flex; flex-direction: column; }
    body.desktop-split-active #mobile-map {
        flex: 1;
        min-height: 0;
        height: auto;
        margin-bottom: 12px;
    }
    body.desktop-split-active .map-gesture-hint,
    body.desktop-split-active .outline-open-map,
    body.desktop-split-active #map-edit-outline,
    body.desktop-split-active #mode-toggle { display: none; }
}

.file-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.file-grid button {
    min-height: 72px;
    padding: 10px 8px;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: var(--surface);
    color: var(--text);
    font-size: 14px;
    font-weight: 500;
}
.file-grid button span { display: block; margin-bottom: 4px; color: var(--accent); font-size: 22px; }
.file-grid .btn-secondary { grid-column: 1 / -1; min-height: 48px; }

.file-grid button:first-child { grid-column: 1 / -1; }

.import-warning { color: #a12828; font-weight: 600; margin-top: 12px; }
.storage-notice-text { color: var(--text); font-size: 15px; line-height: 1.6; margin-bottom: 16px; }

[hidden] { display: none !important; }
