:root {
    --bg: #ffffff;
    --secondary-bg: #f4f4f7;
    --text: #1d1d1f;
    --secondary-text: #6e6e73;
    --accent: #0071e3;
    --accent-hover: #0077ed;
    --border: rgba(0, 0, 0, 0.08);
    --p-glass: rgba(255, 255, 255, 0.75);
    --gutter-bg: #fbfbfd;
    --gutter-text: #a1a1a6;
    --shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
    --metallic-bg: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #161617;
        --secondary-bg: #000000;
        --text: #f5f5f7;
        --secondary-text: #a1a1a6;
        --accent: #2997ff;
        --accent-hover: #40a1ff;
        --border: rgba(255, 255, 255, 0.12);
        --p-glass: rgba(22, 22, 23, 0.82);
        --gutter-bg: #161617;
        --gutter-text: #6e6e73;
        --shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
        --metallic-bg: linear-gradient(135deg, #2c3e50 0%, #000000 100%);
    }
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-font-smoothing: antialiased;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", sans-serif;
    background: var(--metallic-bg);
    color: var(--text);
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    user-select: none;
}

header {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
    padding: 8px 24px;
    min-height: 52px;
    background: var(--p-glass);
    backdrop-filter: saturate(180%) blur(20px);
    border-bottom: 0.5px solid var(--border);
    z-index: 100;
    flex-shrink: 0;
    gap: 12px;
}

.header-left,
.header-center,
.header-right {
    display: flex;
    align-items: center;
    min-height: 36px;
    gap: 12px;
}

.header-center {
    flex: 1;
    justify-content: flex-start;
    min-width: 200px;
}

.title-group {
    display: flex;
    align-items: center;
    gap: 12px;
}

.title {
    font-size: 1.1rem;
    font-weight: 500;
    letter-spacing: -0.01em;
    color: var(--secondary-text);
}

.actions {
    display: flex;
    gap: 20px;
}

.actions-left {
    display: flex;
    gap: 12px;
}

button,
.btn-label {
    background: rgba(255, 255, 255, 0.05);
    border: 0.5px solid var(--border);
    color: var(--accent);
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    padding: 6px 14px;
    border-radius: 980px;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    align-items: center;
    gap: 6px;
    touch-action: manipulation;
    backdrop-filter: blur(10px);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

button:hover,
.btn-label:hover {
    opacity: 1;
    background: var(--accent);
    color: white;
    border-color: var(--accent);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 113, 227, 0.3);
}

button:active,
.btn-label:active {
    transform: scale(0.96) translateY(0);
    box-shadow: 0 2px 4px rgba(0, 113, 227, 0.2);
}

button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    pointer-events: none;
}

.container {
    flex: 1;
    display: flex;
    overflow: hidden;
    background: var(--bg);
    touch-action: none;
    position: relative;
}

.container::before {
    content: "";
    position: absolute;
    inset: 0;
    pointer-events: none;
    border: 1px solid rgba(255, 255, 255, 0.1);
    z-index: 1000;
}

/* Split View */
.pane-editor {
    width: 33.33%;
    min-width: 10vw;
    display: flex;
    flex-direction: column;
    position: relative;
    background: var(--bg);
}

.pane-preview {
    flex: 1;
    min-width: 10vw;
    display: flex;
    flex-direction: column;
    position: relative;
    background: #fff;
}

/* Essential for smooth resizing */
.resizing .pane-preview {
    pointer-events: none;
}

.resizer {
    width: 16px;
    /* Larger hit area for touch */
    margin: 0 -8px;
    cursor: col-resize;
    background: transparent;
    z-index: 100;
    display: flex;
    justify-content: center;
    align-items: center;
    touch-action: none;
}

.resizer::after {
    content: "";
    width: 2px;
    height: 32px;
    background: var(--border);
    border-radius: 1px;
    transition: background 0.3s, height 0.3s;
}

.resizer:hover::after,
.resizing .resizer::after {
    background: var(--accent);
    height: 48px;
}

/* Editor Customization */
.editor-container {
    flex: 1;
    position: relative;
    height: 100%;
    overflow: hidden;
    background: transparent;
}

#editor {
    height: 100%;
    width: 100%;
    font-family: "SF Mono", "Menlo", monospace;
    font-size: 13px;
    line-height: 1.6;
    background: transparent;
    border: none;
    outline: none;
    padding: 10px;
    resize: none;
    color: var(--text);
    white-space: pre;
    /* Default no-wrap, toggled by JS */
    overflow: auto;
}

/* Adjust preview and resizer */
iframe {
    width: 100%;
    height: 100%;
    border: none;
    background: #fff;
}

.label {
    position: absolute;
    top: 10px;
    right: 15px;
    font-size: 9px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: var(--secondary-text);
    pointer-events: none;
    opacity: 0.25;
    z-index: 5;
    display: flex;
    align-items: center;
    gap: 12px;
}

.pane-editor .label {
    pointer-events: auto;
    opacity: 0.8;
    left: auto;
    right: 0px;
    background: var(--p-glass);
    padding: 4px 8px;
    border-radius: 8px;
    backdrop-filter: blur(8px);
    border: 0.5px solid var(--border);
}

.label-tag {
    opacity: 0.6;
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.05em;
    text-transform: uppercase;
}

.label-actions {
    display: flex;
    gap: 6px;
}

.btn-mini {
    font-size: 11px !important;
    padding: 3px 10px !important;
    border-radius: 6px !important;
    height: 24px !important;
    display: flex;
    align-items: center;
    gap: 4px;
}

.btn-full {
    width: 100%;
}

#success-modal h3 {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.dot {
    width: 6px;
    height: 6px;
    background: #34c759;
    border-radius: 50%;
    transform: scale(0);
    transition: transform 0.3s;
}

.dot.visible {
    transform: scale(1);
}

input[type="file"] {
    display: none;
}

@media (max-aspect-ratio: 1/1),
(max-width: 768px) {
    .container {
        flex-direction: column-reverse;
        /* Keep Editor on top for mobile */
    }

    .pane-editor {
        width: 100% !important;
        height: auto;
        max-height: 40vh;
        flex: none;
        min-height: 150px;
    }

    .recent-bar {
        flex-direction: column-reverse;
        /* History bottom, buttons top in this stack? Or just column. Let's start with regular wrapping. User said "bottom auto wrap". Actually if I just let it wrap and rely on flex-wrap that might be enough, but small screens likely need vertical stacking. */
        align-items: stretch;
        gap: 12px;
        padding: 12px;
    }

    .recent-history {
        width: 100%;
    }

    .header-right {
        display: flex;
        justify-content: flex-end;
        width: 100%;
    }

    .pane-preview {
        width: 100% !important;
        min-width: 100%;
        flex: 1;
        /* Take remaining height */
    }

    .resizer {
        width: 100%;
        height: 16px;
        margin: -8px 0;
        cursor: row-resize;
    }

    .resizer::after {
        width: 32px;
        height: 2px;
    }

    .resizer:hover::after,
    .resizing .resizer::after {
        width: 48px;
        height: 2px;
    }
}

.spin {
    animation: spin 1s linear infinite;
}

/* Modal Styles */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(5px);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    opacity: 0;
    transition: opacity 0.3s;
}

.modal-overlay.active {
    display: flex;
    opacity: 1;
}

.modal {
    background: var(--p-glass);
    padding: 32px;
    border-radius: 20px;
    border: 1px solid rgba(255, 255, 255, 0.3);
    box-shadow: var(--shadow), inset 0 0 0 1px rgba(255, 255, 255, 0.1);
    width: 360px;
    transform: scale(0.95);
    transition: transform 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}

.modal-overlay.active .modal {
    transform: scale(1);
}

.modal h3 {
    font-size: 18px;
    margin-bottom: 24px;
    font-weight: 600;
    text-align: center;
    color: var(--text);
}

.modal input {
    width: 100%;
    padding: 14px;
    border: 1px solid var(--border);
    border-radius: 12px;
    margin-bottom: 20px;
    font-size: 16px;
    background: rgba(0, 0, 0, 0.03);
    color: var(--text);
    outline: none;
    text-align: center;
    letter-spacing: 4px;
    font-family: "SF Mono", monospace;
    transition: border-color 0.2s;
}

.modal input:focus {
    border-color: var(--accent);
}

.modal-buttons {
    display: flex;
    gap: 12px;
    justify-content: stretch;
}

.modal-btn {
    flex: 1;
    padding: 12px 16px;
    border-radius: 12px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    text-align: center;
    border: none;
}

.modal-btn-confirm {
    background: var(--accent);
    color: white;
}

.modal-btn-confirm:hover {
    background: var(--accent-hover);
}

.modal-btn-cancel {
    background: rgba(0, 0, 0, 0.05);
    color: var(--text);
}

/* Success Modal Specific */
.success-content {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.success-row {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.success-label {
    font-size: 11px;
    font-weight: 600;
    color: var(--secondary-text);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.success-value-wrapper {
    display: flex;
    gap: 8px;
    background: rgba(0, 0, 0, 0.05);
    padding: 12px 14px;
    border-radius: 12px;
    align-items: center;
    border: 1px solid var(--border);
}

.success-value {
    flex: 1;
    font-family: "SF Mono", monospace;
    font-size: 14px;
    color: var(--text);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.copy-trigger {
    background: var(--accent);
    color: white;
    padding: 4px 10px;
    font-size: 11px;
    border-radius: 6px;
    white-space: nowrap;
    cursor: pointer;
}

.copy-trigger:hover {
    background: var(--accent-hover);
}

.success-footer {
    margin-top: 10px;
    padding-top: 20px;
    border-top: 0.5px solid var(--border);
}

/* Virtual Keypad */
.keypad {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    margin-bottom: 16px;
}

.key {
    padding: 12px;
    background: var(--secondary-bg);
    border-radius: 8px;
    text-align: center;
    font-size: 18px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s, transform 0.1s;
    user-select: none;
}

.key:active {
    background: var(--border);
    transform: scale(0.95);
}

.key.action {
    color: var(--accent);
    font-size: 14px;
}

.modal-notice {
    font-size: 14px;
    color: var(--secondary-text);
    margin-bottom: 16px;
    line-height: 1.4;
    opacity: 0.8;
    text-align: center;
}

/* Toast Styles */
#toast-container {
    position: fixed;
    top: 30px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 2000;
    display: flex;
    flex-direction: column;
    gap: 10px;
    pointer-events: none;
}

.toast {
    background: rgba(0, 0, 0, 0.6);
    color: white;
    padding: 10px 20px;
    border-radius: 8px;
    font-size: 14px;
    backdrop-filter: blur(10px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    animation: toastIn 0.3s ease, toastOut 0.3s ease 2.7s forwards;
    min-width: 200px;
    text-align: center;
}

.toast.error {
    background: rgba(255, 59, 48, 0.9);
}

button:active,
button.active {
    background: var(--accent);
    color: #fff;
    transform: translateY(1px);
}

@keyframes toastIn {
    from {
        transform: translateY(20px);
        opacity: 0;
    }

    to {
        transform: translateY(0);
        opacity: 1;
    }
}

@keyframes toastOut {
    from {
        transform: translateY(0);
        opacity: 1;
    }

    to {
        transform: translateY(-20px);
        opacity: 0;
    }
}

/* Recent Uploads Bar */
.recent-bar {
    max-height: 120px;
    background: var(--p-glass);
    backdrop-filter: blur(20px) saturate(180%);
    border-top: 0.5px solid var(--border);
    display: flex;
    justify-content: space-between;
    padding: 10px 24px 14px;
    font-size: 1rem;
    color: var(--secondary-text);
    gap: 4px 24px;
    flex-shrink: 0;
    overflow-y: auto;
    flex-wrap: wrap;
    align-items: center;
}

.recent-history {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
    flex: 1;
    min-width: 0;
}

.recent-bar::-webkit-scrollbar {
    display: none;
}

.recent-items {
    display: flex;
    gap: 4px 10px;
    align-items: center;
    flex-wrap: wrap;
}

.recent-item {
    color: var(--accent);
    text-decoration: none;
    cursor: pointer;
    transition: opacity 0.2s;
    display: flex;
    align-items: center;
}

.recent-item::before {
    content: "";
    display: inline-block;
    width: 12px;
    height: 12px;
    margin-right: 2px;
    background-color: currentColor;
    opacity: 0.8;
    -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z'/%3E%3Cpolyline points='14 2 14 8 20 8'/%3E%3Cpath d='M10 13l-2 2 2 2'/%3E%3Cpath d='M14 17l2-2-2-2'/%3E%3C/svg%3E") no-repeat center;
    mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z'/%3E%3Cpolyline points='14 2 14 8 20 8'/%3E%3Cpath d='M10 13l-2 2 2 2'/%3E%3Cpath d='M14 17l2-2-2-2'/%3E%3C/svg%3E") no-repeat center;
}

.recent-item:hover {
    opacity: 0.7;
    text-decoration: underline;
}