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

[hidden] {
    display: none !important;
}

:root {
    --left-sidebar-width: 280px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: #1e1e1e;
    color: #fff;
    height: 100vh;
    overflow: hidden;
}

#upload-screen {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
}

.upload-box {
    background: #2d2d2d;
    padding: 2rem;
    border-radius: 8px;
    text-align: center;
    box-shadow: 0 4px 6px rgba(0,0,0,0.3);
}

.upload-box h2 {
    margin-bottom: 1rem;
}

.upload-box input {
    margin-bottom: 1rem;
    display: block;
    width: 100%;
}

button {
    background: #007acc;
    color: white;
    border: none;
    padding: 0.5rem 1rem;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
}

.image-crop-modal {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    z-index: 1000;
}

.editor-modal {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.72);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    z-index: 1100;
}

.editor-modal-dialog {
    width: min(1080px, 100%);
    background: #252526;
    border: 1px solid #3a3a3d;
    border-radius: 8px;
    box-shadow: 0 16px 40px rgba(0, 0, 0, 0.45);
    overflow: hidden;
}

.editor-modal-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    padding: 16px;
    border-bottom: 1px solid #3a3a3d;
}

.editor-modal-header h3 {
    margin: 0;
    font-size: 18px;
}

.editor-modal-header .subtitle {
    padding: 6px 0 0;
    background: none;
}

.editor-modal-body {
    padding: 16px;
}

.element-library-body {
    display: grid;
    grid-template-columns: minmax(280px, 0.95fr) minmax(340px, 1.05fr);
    gap: 16px;
}

.element-library-column {
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.element-library-column-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

.element-library-column-header h4 {
    font-size: 12px;
    text-transform: uppercase;
    color: #9cdcfe;
    letter-spacing: 0.05em;
}

.saved-element-library-list {
    max-height: 62vh;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.saved-element-card,
.element-library-section {
    border: 1px solid #3a3a3d;
    border-radius: 6px;
    background: #202022;
    padding: 12px;
}

.saved-element-card {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.saved-element-title {
    font-size: 13px;
    font-weight: 600;
    color: #f0f0f0;
}

.saved-element-meta,
.library-meta {
    font-size: 12px;
    color: #aaa;
    line-height: 1.4;
}

.saved-element-preview {
    background: #1b1b1d;
    border: 1px solid #333;
    border-radius: 4px;
    padding: 8px;
    font-size: 11px;
    line-height: 1.4;
    color: #d4d4d4;
    white-space: pre-wrap;
    word-break: break-word;
    max-height: 140px;
    overflow: auto;
}

.saved-element-preview-mount {
    display: block;
    pointer-events: none;
}

.saved-element-actions {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
}

.element-library-empty {
    font-size: 12px;
    color: #aaa;
    border: 1px dashed #3a3a3d;
    border-radius: 6px;
    padding: 16px;
    text-align: center;
}

.element-library-section {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.element-library-field {
    display: flex;
    flex-direction: column;
    gap: 6px;
    font-size: 12px;
    color: #ccc;
}

.element-library-field input,
.element-library-field select,
.element-library-field textarea {
    width: 100%;
    background: #1e1e1e;
    color: #d4d4d4;
    border: 1px solid #444;
    border-radius: 4px;
    padding: 8px 10px;
    font-size: 12px;
    outline: none;
}

.element-library-field textarea {
    min-height: 90px;
    resize: vertical;
    font-family: inherit;
}

.element-library-field input:focus,
.element-library-field select:focus,
.element-library-field textarea:focus {
    border-color: #007acc;
}

.image-crop-dialog {
    width: min(920px, 100%);
    background: #252526;
    border: 1px solid #3a3a3d;
    border-radius: 8px;
    box-shadow: 0 16px 40px rgba(0, 0, 0, 0.45);
    overflow: hidden;
}

.image-crop-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 12px;
    padding: 16px;
    border-bottom: 1px solid #3a3a3d;
}

.image-crop-header h3 {
    margin: 0;
    font-size: 18px;
}

.image-crop-body {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 260px;
    gap: 16px;
    padding: 16px;
}

.image-crop-preview-shell {
    background: #1b1b1d;
    border: 1px solid #3a3a3d;
    border-radius: 6px;
    padding: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 360px;
}

.image-crop-viewport {
    position: relative;
    overflow: hidden;
    max-width: 100%;
    max-height: 70vh;
    background:
        linear-gradient(45deg, #2f2f33 25%, transparent 25%, transparent 75%, #2f2f33 75%, #2f2f33),
        linear-gradient(45deg, #2f2f33 25%, transparent 25%, transparent 75%, #2f2f33 75%, #2f2f33);
    background-size: 24px 24px;
    background-position: 0 0, 12px 12px;
    cursor: grab;
    touch-action: none;
    user-select: none;
}

.image-crop-viewport.is-dragging {
    cursor: grabbing;
}

.image-crop-viewport::after {
    content: "";
    position: absolute;
    inset: 0;
    border: 1px solid rgba(255, 255, 255, 0.45);
    pointer-events: none;
}

#image-crop-preview {
    position: absolute;
    top: 50%;
    left: 50%;
    transform-origin: center center;
    max-width: none;
    max-height: none;
    pointer-events: none;
    user-select: none;
    -webkit-user-drag: none;
}

.image-crop-controls {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.image-crop-control {
    display: flex;
    flex-direction: column;
    gap: 8px;
    font-size: 12px;
    color: #d4d4d4;
}

.image-crop-control input[type="range"] {
    width: 100%;
}

.image-crop-meta {
    display: flex;
    flex-direction: column;
    gap: 8px;
    font-size: 12px;
    color: #aaa;
}

.image-crop-actions {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: auto;
}

.image-crop-actions button,
#image-crop-close-btn {
    width: 100%;
}

.image-crop-header #image-crop-close-btn {
    width: auto;
}

button:hover {
    background: #005999;
}

#editor-screen {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

.header {
    height: 50px;
    background: #333;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 1rem;
    border-bottom: 1px solid #444;
}

.editor-container {
    display: flex;
    flex: 1;
    overflow: hidden;
}

.sidebar {
    width: 250px;
    background: #252526;
    display: flex;
    flex-direction: column;
    border-right: 1px solid #444;
}

.sidebar.left {
    flex: 0 0 var(--left-sidebar-width);
    width: var(--left-sidebar-width);
    min-width: 220px;
    max-width: 680px;
    overflow: hidden;
}

.sidebar.right {
    border-right: none;
    border-left: 1px solid #444;
    width: 320px;
    overflow-y: auto;
}

.sidebar h3 {
    padding: 10px;
    background: #333;
    font-size: 14px;
    text-transform: uppercase;
    position: sticky;
    top: 0;
    z-index: 10;
}

.sidebar-toolbar {
    padding: 10px;
    background: #333;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
}

.sidebar-toolbar-title {
    padding: 0;
    background: none;
}

.sidebar-toolbar-actions {
    display: flex;
    align-items: center;
    gap: 10px;
}

.sidebar-toggle {
    font-size: 11px;
    cursor: pointer;
    color: #ddd;
}

#tree-container {
    flex: 1;
    overflow-y: auto;
    padding: 10px;
    font-size: 13px;
}

.sidebar-resizer {
    flex: 0 0 8px;
    cursor: col-resize;
    background: linear-gradient(to right, transparent 0, transparent 2px, #3a3a3d 2px, #3a3a3d 3px, transparent 3px, transparent 100%);
    position: relative;
}

.sidebar-resizer:hover,
body.is-resizing-sidebar .sidebar-resizer {
    background: linear-gradient(to right, transparent 0, transparent 2px, #007acc 2px, #007acc 4px, transparent 4px, transparent 100%);
}

body.is-resizing-sidebar {
    cursor: col-resize;
    user-select: none;
}

#left-style-editor {
    border-top: 1px solid #444;
    background: #202022;
}

#left-image-manager {
    border-top: 1px solid #444;
    background: #202022;
}

#left-style-editor > summary {
    list-style: none;
    cursor: pointer;
    padding: 10px;
    font-size: 12px;
    text-transform: uppercase;
    color: #ddd;
    background: #2d2d30;
    user-select: none;
}

#left-style-editor > summary::-webkit-details-marker {
    display: none;
}

#left-style-editor > summary::before {
    content: "▸";
    display: inline-block;
    margin-right: 8px;
}

#left-style-editor[open] > summary::before {
    content: "▾";
}

#style-tag-panels {
    padding: 10px;
}

#left-image-manager > summary {
    list-style: none;
    cursor: pointer;
    padding: 10px;
    font-size: 12px;
    text-transform: uppercase;
    color: #ddd;
    background: #2d2d30;
    user-select: none;
}

#left-image-manager > summary::-webkit-details-marker {
    display: none;
}

#left-image-manager > summary::before {
    content: "▾";
    display: inline-block;
    margin-right: 8px;
}

#left-image-manager:not([open]) > summary::before {
    content: "▸";
}

#project-image-list {
    padding: 10px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    max-height: 340px;
    overflow-y: auto;
}

.project-image-empty {
    font-size: 12px;
    color: #aaa;
}

.project-image-card {
    border: 1px solid #3a3a3d;
    border-radius: 4px;
    overflow: hidden;
    background: #252526;
    flex: 0 0 auto;
}

.project-image-preview {
    display: block;
    width: 100%;
    max-height: 140px;
    object-fit: contain;
    background: #1b1b1d;
    border-bottom: 1px solid #3a3a3d;
}

.project-image-card-body {
    padding: 10px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.project-image-name {
    font-size: 12px;
    font-weight: 600;
    color: #f0f0f0;
    word-break: break-word;
}

.project-image-meta {
    font-size: 11px;
    color: #aaa;
}

.project-image-actions {
    display: flex;
    justify-content: flex-end;
}

.project-image-card.is-loading {
    opacity: 0.65;
}

.style-tag-panel {
    margin-bottom: 10px;
    border: 1px solid #3a3a3d;
    border-radius: 4px;
    overflow: hidden;
    background: #252526;
}

.style-tag-panel > summary {
    list-style: none;
    cursor: pointer;
    padding: 8px 10px;
    font-size: 12px;
    color: #d4d4d4;
    background: #2f2f33;
    user-select: none;
}

.style-tag-panel > summary::-webkit-details-marker {
    display: none;
}

.style-tag-panel > summary::before {
    content: "▸";
    display: inline-block;
    margin-right: 8px;
}

.style-tag-panel[open] > summary::before {
    content: "▾";
}

.style-tag-panel-body {
    padding: 10px;
}

.style-tag-panel-body .css-code-editor {
    margin: 0;
}

.style-tag-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 8px;
}

.style-tag-actions button,
.css-editor-actions button {
    white-space: nowrap;
}

.style-tag-empty {
    font-size: 12px;
    color: #aaa;
}

.tree-node {
    padding: 2px 0;
    cursor: pointer;
    white-space: nowrap;
}

.tree-node:hover {
    background: #37373d;
}

.tree-node.selected {
    background: #094771;
}

.tree-node-content {
    display: flex;
    align-items: center;
}

.tree-node-tag {
    color: #569cd6;
    margin-right: 5px;
}

.tree-node-id {
    color: #d7ba7d;
}

.tree-node-class {
    color: #ce9178;
}

.main-view {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    background: #1e1e1e;
    overflow: auto;
    position: relative;
}

#ad-frame {
    background: white;
    border: none;
    box-shadow: 0 0 10px rgba(0,0,0,0.5);
    /* Dimensions will be controlled by ad's meta tag or default */
    width: 300px;
    height: 250px;
    resize: both;
    overflow: auto;
}

#css-editor {
    padding: 0;
    border-top: 1px solid #444;
    display: flex;
    flex-direction: column;
    min-height: 200px;
}

.css-editor-actions {
    display: flex;
    gap: 8px;
    padding: 10px;
    padding-bottom: 0;
}

.css-code-editor {
    --css-editor-min-height: 160px;
    position: relative;
    min-height: var(--css-editor-min-height);
    margin: 10px;
    margin-top: 0;
    border: 1px solid #444;
    border-radius: 4px;
    background: #1e1e1e;
    overflow: hidden;
}

.css-code-editor:focus-within {
    border-color: #007acc;
}

.css-code-editor.is-empty::after {
    content: attr(data-placeholder);
    position: absolute;
    top: 8px;
    left: 10px;
    right: 10px;
    color: #6b7280;
    font-family: "Courier New", Courier, monospace;
    font-size: 12px;
    pointer-events: none;
    white-space: pre-wrap;
}

.css-code-highlight,
.css-code-input {
    width: 100%;
    min-height: var(--css-editor-min-height);
    margin: 0;
    padding: 8px 10px;
    border: none;
    font-family: "Courier New", Courier, monospace;
    font-size: 12px;
    line-height: 1.5;
    tab-size: 4;
    white-space: pre;
    overflow-wrap: normal;
    word-break: normal;
    letter-spacing: normal;
    font-kerning: none;
    font-variant-ligatures: none;
    text-rendering: geometricPrecision;
}

.css-code-highlight {
    position: absolute;
    inset: 0;
    color: #d4d4d4;
    pointer-events: none;
    overflow: hidden;
}

.css-code-highlight code {
    display: block;
    min-height: calc(var(--css-editor-min-height) - 16px);
}

.css-code-highlight code,
.css-code-highlight span {
    font-family: inherit;
    font-size: inherit;
    line-height: inherit;
    letter-spacing: inherit;
    font-weight: inherit;
    font-style: inherit;
    font-kerning: inherit;
    font-variant-ligatures: inherit;
    text-rendering: inherit;
    tab-size: inherit;
    white-space: inherit;
}

.css-code-input {
    position: relative;
    z-index: 1;
    resize: none;
    background: transparent;
    color: transparent;
    caret-color: #d4d4d4;
    outline: none;
    overflow: auto;
    -webkit-text-fill-color: transparent;
}

.css-code-input,
.css-code-highlight {
    font-weight: 400;
    font-style: normal;
}

.css-code-input::selection {
    background: rgba(0, 122, 204, 0.35);
}

.css-code-input::placeholder {
    color: transparent;
}

.css-token-selector {
    color: #d7ba7d;
}

.css-token-property {
    color: #9cdcfe;
}

.css-token-value {
    color: #ce9178;
}

.css-token-number {
    color: #b5cea8;
}

.css-token-function {
    color: #dcdcaa;
}

.css-token-at-rule {
    color: #c586c0;
}

.css-token-string {
    color: #ce9178;
}

.css-token-comment {
    color: #6a9955;
}

.css-token-important {
    color: #f44747;
}

/* Element Controls */
.control-group {
    padding: 10px;
    border-bottom: 1px solid #444;
}

.control-group h4 {
    font-size: 12px;
    color: #007acc;
    margin-bottom: 8px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.ctrl-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 8px;
    font-size: 12px;
}

.ctrl-row-stack {
    align-items: flex-start;
}

.ctrl-row label {
    flex: 0 0 60px;
    color: #ccc;
}

.ctrl-check-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin-top: 10px;
    font-size: 12px;
    color: #ccc;
    cursor: pointer;
}

.ctrl-check-row input[type="checkbox"] {
    width: 16px;
    height: 16px;
    accent-color: #007acc;
}

.image-upload-meta {
    margin-bottom: 10px;
    font-size: 12px;
    line-height: 1.4;
    color: #aaa;
    word-break: break-word;
}

.filter-editor {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.filter-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.filter-empty {
    padding: 10px;
    border: 1px dashed #444;
    border-radius: 4px;
    font-size: 12px;
    color: #888;
}

.filter-item {
    border: 1px solid #3a3a3d;
    border-radius: 4px;
    background: #1f1f21;
    padding: 8px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.filter-item-main,
.filter-item-actions {
    display: flex;
    gap: 8px;
    align-items: center;
}

.filter-item-main select,
.filter-item-main input {
    flex: 1;
    min-width: 0;
}

.filter-item-actions {
    justify-content: flex-end;
    display: none;
}

.filter-item-actions button {
    min-width: 32px;
    padding: 6px 8px;
}

.filter-item.is-active .filter-item-actions {
    display: flex;
}

.ctrl-row input[type="text"], 
.ctrl-row select,
.ctrl-joystick input[type="text"] {
    background: #1e1e1e;
    color: #d4d4d4;
    border: 1px solid #444;
    border-radius: 3px;
    padding: 4px 6px;
    font-size: 12px;
    outline: none;
}

.ctrl-row input[type="text"]:focus,
.ctrl-row select:focus,
.ctrl-joystick input[type="text"]:focus {
    border-color: #007acc;
}

.ctrl-row input[type="text"] {
    flex: 1;
    width: 100%;
}

.ctrl-row select {
    flex: 1;
    width: 100%;
}

.metric-input-wrap {
    display: flex;
    flex: 1;
    gap: 6px;
    align-items: center;
}

.metric-input-wrap input[type="text"] {
    flex: 1;
}

.metric-input-wrap select {
    flex: 0 0 72px;
}

.color-picker-wrap {
    display: flex;
    flex: 1;
    gap: 5px;
    align-items: center;
}

.color-picker-wrap input[type="color"] {
    width: 24px;
    height: 24px;
    padding: 0;
    border: 1px solid #444;
    border-radius: 3px;
    background: none;
    cursor: pointer;
}

.color-picker-wrap input[type="text"] {
    flex: 1;
}

.link-btn {
    background: none;
    padding: 2px 6px;
    font-size: 14px;
    opacity: 0.5;
}

.link-btn.active {
    opacity: 1;
    background: #007acc;
}

.ctrl-joystick {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    margin-bottom: 12px;
    background: #252526;
    padding: 8px;
    border-radius: 4px;
    border: 1px solid #333;
}

.joy-top, .joy-bot {
    display: flex;
    justify-content: center;
    width: 100%;
}

.joy-mid {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 4px;
    width: 100%;
}

.joy-center {
    width: 24px;
    text-align: center;
    font-size: 10px;
    color: #666;
    font-weight: bold;
}

.ctrl-joystick input {
    width: 50px !important;
    text-align: center;
}

.text-content-item {
    margin-top: 10px;
    padding: 8px;
    background: #252526;
    border: 1px solid #333;
    border-radius: 4px;
}

.text-content-label {
    display: block;
    margin-bottom: 6px;
    font-size: 11px;
    color: #9cdcfe;
}

.text-content-input {
    width: 100%;
    min-height: 56px;
    resize: vertical;
    background: #1e1e1e;
    color: #d4d4d4;
    border: 1px solid #444;
    border-radius: 3px;
    padding: 6px 8px;
    font-size: 12px;
    font-family: inherit;
    outline: none;
}

.text-content-input:focus {
    border-color: #007acc;
}

.phase-controls {
    border-bottom: 1px solid #444;
}

.phase-controls-row {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px;
}

.phase-select-label {
    font-size: 12px;
    color: #ccc;
}

#phase-select {
    margin: 0 10px 10px;
    width: calc(100% - 20px);
    background: #1e1e1e;
    color: #d4d4d4;
    border: 1px solid #444;
    border-radius: 4px;
    padding: 8px;
}

#phase-toggle-btn[disabled],
#phase-select[disabled] {
    opacity: 0.6;
    cursor: not-allowed;
}

.subtitle {
    padding: 10px;
    font-size: 12px;
    color: #ccc;
    background: #2d2d2d;
    word-wrap: break-word;
}

#css-editor .css-code-editor {
    --css-editor-min-height: 240px;
    flex: 1;
    min-height: 240px;
}

.css-editor-modal-dialog {
    width: min(1180px, 100%);
}

.css-editor-modal-body {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

#css-editor-modal-textarea {
    min-height: 72vh;
}

#css-editor-modal .css-code-editor {
    --css-editor-min-height: 72vh;
    margin: 0;
}

/* Move Into Mode */
body.move-into-mode {
    cursor: crosshair !important;
}

.tree-node.move-target:hover {
    outline: 2px dashed #007acc;
    outline-offset: -2px;
}
