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

: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;
}

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-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-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;
}

.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-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-wrap;
    overflow-wrap: break-word;
}

.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-input {
    position: relative;
    z-index: 1;
    resize: vertical;
    background: transparent;
    color: transparent;
    caret-color: #d4d4d4;
    outline: none;
    overflow: auto;
    -webkit-text-fill-color: transparent;
}

.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 label {
    flex: 0 0 60px;
    color: #ccc;
}

.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;
}

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

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