@import url('https://fonts.googleapis.com/css2?family=Cinzel+Decorative:wght@400;700;900&family=Crimson+Pro:ital,wght@0,300;0,400;0,600;1,400&display=swap');

:root {
    --bg: #0d0a07;
    --surface: #1a1410;
    --surface2: #221c15;
    --surface3: #2e2419;
    --gold: #c9943a;
    --gold2: #e8b84b;
    --gold-dim: #7a5a1f;
    --red: #8b2020;
    --red2: #c23030;
    --green: #1a4a2a;
    --green2: #2d7a45;
    --blue: #1a2a4a;
    --blue2: #2d457a;
    --purple: #2a1a4a;
    --purple2: #4a2d7a;
    --text: #e8dcc8;
    --text-dim: #8a7a60;
    --border: #3a2e1e;
    --shadow: 0 4px 24px rgba(0,0,0,0.7);
    --radius: 8px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
    background: var(--bg);
    color: var(--text);
    font-family: 'Crimson Pro', Georgia, serif;
    font-size: 16px;
    min-height: 100vh;
    overflow-x: hidden;
}

body::before {
    content: '';
    position: fixed;
    inset: 0;
    background-image:
    radial-gradient(ellipse at 20% 20%, rgba(201,148,58,0.04) 0%, transparent 50%),
    radial-gradient(ellipse at 80% 80%, rgba(139,32,32,0.04) 0%, transparent 50%);
    pointer-events: none;
    z-index: 0;
}

#app { display: flex; flex-direction: column; min-height: 100vh; position: relative; z-index: 1; }

.main-layout { display: flex; flex: 1; overflow: hidden; }

/* ─── SIDEBAR ─── */
#sidebar {
    width: 260px;
    min-width: 220px;
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    flex-shrink: 0;
}

.sidebar-section { border-bottom: 1px solid var(--border); padding: 0; }

.sidebar-header {
    padding: 10px 14px;
    font-family: 'Cinzel Decorative', serif;
    font-size: 0.65rem;
    color: var(--gold);
    letter-spacing: 2px;
    text-transform: uppercase;
    background: var(--surface2);
    cursor: pointer;
    display: flex; justify-content: space-between; align-items: center;
    user-select: none;
}
.sidebar-header:hover { background: var(--surface3); }
.sidebar-toggle { transition: transform 0.2s; font-size: 0.8rem; }
.collapsed .sidebar-toggle { transform: rotate(-90deg); }

.sidebar-body { padding: 10px 12px; display: flex; flex-direction: column; gap: 6px; }
.sidebar-body.hidden { display: none; }

/* ─── CANVAS AREA ─── */
#canvas-wrap {
    flex: 1;
    overflow: auto;
    position: relative;
    background: var(--bg);
}

#canvas-wrap::before {
    content: '';
    position: absolute;
    inset: 0;
    background-image:
    linear-gradient(rgba(201,148,58,0.04) 1px, transparent 1px),
    linear-gradient(90deg, rgba(201,148,58,0.04) 1px, transparent 1px);
    background-size: 40px 40px;
    pointer-events: none;
}

#board-canvas {
    position: relative;
    min-width: 1200px;
    min-height: 900px;
    width: max-content;
    height: max-content;
}

/* SVG overlay for lines */
#lines-svg {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    pointer-events: none;
    overflow: visible;
    z-index: 50;
}

.conn-line {
    stroke: var(--gold-dim);
    stroke-width: 2;
    fill: none;
    pointer-events: stroke;
    cursor: pointer;
    stroke-dasharray: none;
    transition: stroke 0.15s;
}
.conn-line:hover { stroke: var(--gold2); stroke-width: 3; }
.conn-line.selected-line { stroke: var(--gold2); stroke-width: 3; }

.conn-label {
    fill: var(--gold-dim);
    font-family: 'Crimson Pro', serif;
    font-size: 11px;
    text-anchor: middle;
    pointer-events: none;
    user-select: none;
}

/* ─── BOARD ELEMENTS ─── */
.board-element {
    position: absolute;
    cursor: grab;
    user-select: none;
    transition: box-shadow 0.15s;
}
.board-element:active { cursor: grabbing; }
.board-element.selected { outline: 2px solid var(--gold2); outline-offset: 2px; }
.board-element.z-locked { cursor: default; }
.board-element.z-locked:active { cursor: default; }

/* Connect mode: highlight connectable elements */
body.connect-mode .board-element {
    cursor: crosshair;
    outline: 1px dashed var(--gold-dim);
    outline-offset: 2px;
}
body.connect-mode .board-element.connect-source {
    outline: 2px solid var(--gold2);
    outline-offset: 2px;
    cursor: crosshair;
}
body.connect-mode .board-element:hover {
    outline: 2px solid var(--gold2);
    outline-offset: 2px;
}

.board-element .el-label {
    font-family: 'Cinzel Decorative', serif;
    font-size: 0.55rem;
    color: var(--gold-dim);
    text-align: center;
    margin-top: 4px;
    letter-spacing: 1px;
    pointer-events: none;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 120px;
}

.board-element .el-inner {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    pointer-events: none;
}

.board-element .el-emoji { font-size: 1.8rem; line-height: 1; }
.board-element .el-text {
    font-size: 0.7rem;
    color: var(--text);
    text-align: center;
    padding: 2px 4px;
    word-break: break-word;
}

/* Z-lock indicator */
.z-lock-badge {
    position: absolute;
    top: 2px;
    left: 2px;
    font-size: 0.6rem;
    opacity: 0.6;
    pointer-events: none;
    line-height: 1;
}

/* Element type styles */
.el-space {
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid;
    min-width: 70px;
    min-height: 70px;
    padding: 6px;
}
.el-character {
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 3px solid;
    width: 70px;
    height: 70px;
}
.el-item {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 50px;
    height: 50px;
    border-radius: 4px;
    border: 2px solid;
}
.el-card {
    width: 80px;
    height: 110px;
    border-radius: 8px;
    border: 2px solid;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 6px;
}
.el-token {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 2px solid;
    display: flex;
    align-items: center;
    justify-content: center;
}
.el-zone {
    min-width: 150px;
    min-height: 100px;
    border-radius: 8px;
    border: 2px dashed;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 10px;
}
.el-text-block {
    min-width: 80px;
    padding: 8px 10px;
    border-radius: 4px;
    border: 1px solid var(--border);
    background: var(--surface2);
}
.el-custom {
    min-width: 60px;
    min-height: 60px;
    border-radius: 6px;
    border: 2px solid;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 6px;
}

/* Context menu */
#context-menu {
    position: fixed;
    background: var(--surface2);
    border: 1px solid var(--gold-dim);
    border-radius: 6px;
    padding: 4px;
    z-index: 1000;
    min-width: 160px;
    box-shadow: var(--shadow);
    display: none;
}
#context-menu .cm-item {
    padding: 7px 12px;
    cursor: pointer;
    border-radius: 4px;
    font-size: 0.85rem;
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--text);
}
#context-menu .cm-item:hover { background: var(--surface3); color: var(--gold2); }
#context-menu .cm-divider { border-top: 1px solid var(--border); margin: 3px 0; }
#context-menu .cm-danger:hover { background: rgba(139,32,32,0.3); color: #ff6060; }

/* ─── MODALS ─── */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.8);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 2000;
    padding: 20px;
}
.modal-overlay.open { display: flex; }

.modal {
    background: var(--surface);
    border: 1px solid var(--gold-dim);
    border-radius: 12px;
    padding: 0;
    max-width: 560px;
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: 0 0 60px rgba(201,148,58,0.15);
}

.modal-header {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--surface2);
    border-radius: 12px 12px 0 0;
    position: sticky;
    top: 0;
    z-index: 1;
}
.modal-header h2 {
    font-family: 'Cinzel Decorative', serif;
    font-size: 1rem;
    color: var(--gold2);
    flex: 1;
}
.modal-body { padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.modal-footer { padding: 14px 20px; border-top: 1px solid var(--border); display: flex; gap: 8px; justify-content: flex-end; }

/* ─── CONNECTION MODAL ─── */
.modal.modal-sm { max-width: 380px; }

/* ─── FORM CONTROLS ─── */
label { font-size: 0.8rem; color: var(--text-dim); margin-bottom: 3px; display: block; }

input[type=text], input[type=number], textarea, select {
    background: var(--surface3);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text);
    padding: 8px 10px;
    font-family: 'Crimson Pro', serif;
    font-size: 0.95rem;
    width: 100%;
    outline: none;
    transition: border-color 0.2s;
}
input[type=text]:focus, input[type=number]:focus, textarea:focus, select:focus { border-color: var(--gold-dim); }
select option { background: var(--surface3); }
textarea { resize: vertical; min-height: 60px; }
input[type=color] {
    width: 40px;
    height: 32px;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: none;
    cursor: pointer;
    padding: 2px;
}
input[type=checkbox] {
    width: 16px; height: 16px;
    accent-color: var(--gold);
    cursor: pointer;
    vertical-align: middle;
    margin-right: 6px;
}

.form-row { display: flex; gap: 10px; align-items: flex-end; }
.form-row > * { flex: 1; }
.form-group { display: flex; flex-direction: column; gap: 4px; }
.checkbox-row { display: flex; align-items: center; gap: 6px; padding: 6px 0; }
.checkbox-row label { margin: 0; font-size: 0.9rem; color: var(--text); cursor: pointer; }

/* ─── BUTTONS ─── */
.btn {
    padding: 8px 14px;
    border-radius: 6px;
    border: none;
    cursor: pointer;
    font-family: 'Cinzel Decorative', serif;
    font-size: 0.6rem;
    letter-spacing: 1px;
    transition: all 0.15s;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    white-space: nowrap;
}
.btn-gold { background: linear-gradient(135deg, var(--gold-dim), var(--gold)); color: #0a0804; }
.btn-gold:hover { filter: brightness(1.15); transform: translateY(-1px); }
.btn-ghost { background: transparent; color: var(--text-dim); border: 1px solid var(--border); }
.btn-ghost:hover { border-color: var(--gold-dim); color: var(--gold); }
.btn-danger { background: var(--red); color: #ffcccc; }
.btn-danger:hover { background: var(--red2); }
.btn-active { background: rgba(201,148,58,0.2); color: var(--gold2); border: 1px solid var(--gold-dim); }
.btn-sm { padding: 5px 10px; font-size: 0.55rem; }
.btn-icon { width: 30px; height: 30px; padding: 0; justify-content: center; font-size: 0.75rem; font-family: inherit; }

.sidebar-btn {
    background: var(--surface3);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text);
    padding: 8px 10px;
    cursor: pointer;
    font-family: 'Crimson Pro', serif;
    font-size: 0.9rem;
    text-align: left;
    width: 100%;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: all 0.15s;
}
.sidebar-btn:hover { background: var(--surface3); border-color: var(--gold-dim); color: var(--gold2); }
.sidebar-btn .sb-icon { font-size: 1.1rem; }
.sidebar-btn.active-mode { border-color: var(--gold2); color: var(--gold2); background: rgba(201,148,58,0.1); }

/* ─── WHEELS PANEL ─── */
#wheels-panel {
    position: fixed;
    right: 0; top: 0;
    height: 100vh;
    width: 380px;
    background: var(--surface);
    border-left: 1px solid var(--border);
    z-index: 500;
    display: flex;
    flex-direction: column;
    transform: translateX(100%);
    transition: transform 0.3s ease;
    box-shadow: -8px 0 40px rgba(0,0,0,0.7);
}
#wheels-panel.open { transform: translateX(0); }

.wheels-header {
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
    background: var(--surface2);
    display: flex;
    align-items: center;
    gap: 10px;
}
.wheels-header h2 { font-family: 'Cinzel Decorative', serif; font-size: 0.85rem; color: var(--gold2); flex: 1; }
.wheels-body { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 14px; }

/* ─── NOTES PANEL ─── */
#notes-panel {
    position: fixed;
    left: 260px; top: 0;
    height: 100vh;
    width: 320px;
    background: var(--surface);
    border-right: 1px solid var(--border);
    z-index: 400;
    display: flex;
    flex-direction: column;
    transform: translateX(calc(-100% - 260px));
    transition: transform 0.3s ease;
    box-shadow: 8px 0 40px rgba(0,0,0,0.7);
}
#notes-panel.open { transform: translateX(0); }

.notes-header {
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
    background: var(--surface2);
    display: flex;
    align-items: center;
    gap: 10px;
}
.notes-header h2 { font-family: 'Cinzel Decorative', serif; font-size: 0.85rem; color: var(--gold2); flex: 1; }

.notes-char-tabs {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    padding: 8px 12px;
    border-bottom: 1px solid var(--border);
    background: var(--surface2);
}

.notes-char-tab {
    padding: 4px 10px;
    border-radius: 99px;
    background: var(--surface3);
    border: 1px solid var(--border);
    font-size: 0.78rem;
    cursor: pointer;
    color: var(--text-dim);
    transition: all 0.15s;
    display: flex;
    align-items: center;
    gap: 4px;
}
.notes-char-tab.active { border-color: var(--gold-dim); color: var(--gold); background: rgba(201,148,58,0.08); }
.notes-char-tab:hover { border-color: var(--gold-dim); color: var(--gold2); }

.notes-body { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 12px; }

.notes-empty { color: var(--text-dim); font-size: 0.85rem; text-align: center; padding: 30px 0; }

.notes-char-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--border);
}
.notes-char-emoji { font-size: 1.5rem; }
.notes-char-name { font-family: 'Cinzel Decorative', serif; font-size: 0.8rem; color: var(--gold2); flex: 1; }

.notes-section-label {
    font-family: 'Cinzel Decorative', serif;
    font-size: 0.55rem;
    color: var(--gold-dim);
    letter-spacing: 2px;
    text-transform: uppercase;
    margin-bottom: 4px;
}

.notes-textarea {
    background: var(--surface3);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text);
    padding: 8px 10px;
    font-family: 'Crimson Pro', serif;
    font-size: 0.9rem;
    width: 100%;
    outline: none;
    resize: vertical;
    min-height: 80px;
    transition: border-color 0.2s;
}
.notes-textarea:focus { border-color: var(--gold-dim); }

.notes-save-btn {
    padding: 7px 14px;
    border-radius: 6px;
    border: none;
    cursor: pointer;
    background: linear-gradient(135deg, var(--gold-dim), var(--gold));
    color: #0a0804;
    font-family: 'Cinzel Decorative', serif;
    font-size: 0.55rem;
    letter-spacing: 1px;
    align-self: flex-end;
    transition: all 0.15s;
}
.notes-save-btn:hover { filter: brightness(1.15); }

.notes-stat-row { display: flex; gap: 6px; align-items: center; }
.notes-stat-row input { flex: 1; }
.notes-stat-row .stat-val {
    width: 50px;
    text-align: center;
}

/* ─── Wheel card ─── */
.wheel-card {
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 12px;
}
.wheel-card-header { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.wheel-card-name { font-family: 'Cinzel Decorative', serif; font-size: 0.7rem; color: var(--gold); flex: 1; }
.wheel-canvas-wrap { display: flex; justify-content: center; margin-bottom: 10px; }
canvas.wheel-canvas { border-radius: 50%; box-shadow: 0 0 20px rgba(201,148,58,0.2); }
.wheel-result { text-align: center; min-height: 28px; font-size: 1rem; color: var(--gold2); font-weight: 600; padding: 4px; border-radius: 4px; background: var(--surface3); margin-bottom: 8px; }
.wheel-entries { display: flex; flex-direction: column; gap: 4px; max-height: 140px; overflow-y: auto; margin-bottom: 8px; }
.wheel-entry { display: flex; align-items: center; gap: 6px; padding: 4px 6px; background: var(--surface3); border-radius: 4px; font-size: 0.85rem; }
.wheel-entry .we-color { width: 12px; height: 12px; border-radius: 3px; flex-shrink: 0; }
.wheel-entry .we-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.wheel-entry .we-del { cursor: pointer; color: var(--text-dim); font-size: 0.8rem; flex-shrink: 0; }
.wheel-entry .we-del:hover { color: #ff6060; }
.wheel-add-row { display: flex; gap: 6px; }
.wheel-add-row input { font-size: 0.85rem; padding: 6px 8px; }

/* ─── CHIPS ─── */
.chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chip { padding: 3px 10px; border-radius: 99px; background: var(--surface3); border: 1px solid var(--border); font-size: 0.8rem; cursor: pointer; transition: all 0.15s; color: var(--text-dim); }
.chip:hover, .chip.active { border-color: var(--gold-dim); color: var(--gold); background: rgba(201,148,58,0.08); }

/* ─── RESIZE HANDLE ─── */
.resize-handle { position: absolute; bottom: 2px; right: 2px; width: 12px; height: 12px; cursor: se-resize; color: var(--gold-dim); font-size: 10px; line-height: 1; opacity: 0; transition: opacity 0.2s; pointer-events: all; }
.board-element:hover .resize-handle, .board-element.selected .resize-handle { opacity: 1; }
.z-locked .resize-handle { display: none; }

/* ─── TOAST ─── */
#toast {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%) translateY(60px);
    background: var(--surface2);
    border: 1px solid var(--gold-dim);
    color: var(--gold2);
    padding: 10px 20px;
    border-radius: 99px;
    font-size: 0.9rem;
    z-index: 9999;
    transition: transform 0.3s;
    pointer-events: none;
}
#toast.show { transform: translateX(-50%) translateY(0); }

/* connect mode banner */
#connect-banner {
    position: fixed;
    top: 10px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(201,148,58,0.15);
    border: 1px solid var(--gold-dim);
    color: var(--gold2);
    padding: 8px 20px;
    border-radius: 99px;
    font-size: 0.85rem;
    z-index: 9998;
    pointer-events: none;
    display: none;
}
#connect-banner.show { display: block; }

::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--surface); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--gold-dim); }