:root {
    --bg-app: #E6E8ED;
    --bg-white: #FFFFFF;
    --bg-surface: #F3F4F6;
    --sidebar-bg: #EAECEF;
    
    --text-main: #111827;
    --text-muted: #6B7280;
    --text-sub: #9CA3AF;
    
    --primary: #4F46E5;
    --primary-hover: #4338CA;
    --primary-light: #E0E7FF;
    
    --success: #10B981;
    --danger: #EF4444;
    --border: #E5E7EB;
    
    --box-bg: rgba(79, 70, 229, 0.15);
    --box-border: #4F46E5;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    padding: 0;
    font-family: 'Inter', sans-serif;
    background-color: var(--bg-app);
    color: var(--text-main);
    overflow: hidden; /* we manage scroll inside containers */
}

/* Modal styling */
.modal-overlay {
    position: fixed;
    top: 0; left: 0; width: 100vw; height: 100vh;
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(2px);
    display: flex; justify-content: center; align-items: center;
    z-index: 100;
}
.modal-card {
    background: var(--bg-white);
    padding: 1.5rem;
    border-radius: 12px;
    width: 400px;
    box-shadow: 0 10px 25px rgba(0,0,0,0.1);
}
.modal-input {
    width: 100%;
    padding: 0.75rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    outline: none;
    font-family: 'Inter', sans-serif;
    margin-bottom: 1.5rem;
}
.modal-input:focus { border-color: var(--primary); }
.modal-actions {
    display: flex; justify-content: flex-end; gap: 0.5rem;
}

.hidden { display: none !important; }

/* GLOBAL LAYOUT */
.app-layout {
    display: flex;
    height: 100vh;
    width: 100vw;
}

/* SIDEBAR */
.global-sidebar {
    width: 260px;
    background-color: var(--sidebar-bg);
    display: flex;
    flex-direction: column;
    padding: 1.5rem;
}
.sidebar-logo {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 2rem;
}
.logo-icon {
    width: 40px; height: 40px;
    background: var(--primary);
    border-radius: 10px;
    display: flex; justify-content: center; align-items: center;
}
.logo-text strong {
    font-size: 1rem;
    display: block;
    line-height: 1.2;
}
.logo-text span {
    font-size: 0.65rem;
    letter-spacing: 0.05em;
    color: var(--text-muted);
}

.sidebar-nav {
    display: flex; flex-direction: column; gap: 0.25rem;
    flex: 1;
}
.nav-section-title {
    font-size: 0.65rem;
    font-weight: 600;
    color: var(--text-sub);
    letter-spacing: 0.05em;
    margin-bottom: 0.5rem;
    margin-left: 0.5rem;
}
.nav-item {
    display: flex; align-items: center; gap: 0.75rem;
    padding: 0.75rem;
    border-radius: 8px;
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-family: 'Inter', sans-serif;
    font-weight: 500;
    font-size: 0.875rem;
    cursor: pointer;
    text-align: left;
    transition: all 0.2s;
}
.nav-item:hover {
    background: rgba(0,0,0,0.05);
}
.nav-item.active {
    background: var(--primary-light);
    color: var(--primary);
}
.sidebar-user {
    display: flex; align-items: center; gap: 0.75rem;
    padding: 0.5rem;
    border-radius: 8px;
}
.user-avatar {
    width: 32px; height: 32px;
    background: #60A5FA;
    color: white;
    border-radius: 6px;
    display: flex; justify-content: center; align-items: center;
    font-weight: 600; font-size: 0.75rem;
}
.user-name { font-size: 0.875rem; font-weight: 600; }

/* MAIN AREA WRAPPER */
.global-main-wrap {
    flex: 1;
    padding: 1.5rem 1.5rem 1.5rem 0;
    display: flex;
}
.global-main-content {
    background: var(--bg-white);
    border-radius: 16px;
    flex: 1;
    box-shadow: 0 4px 6px -1px rgba(0,0,0,0.05);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    /* optional border: 1px solid var(--border); */
}

/* TOP NAV */
.top-nav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem 1.5rem;
    border-bottom: 1px solid var(--border);
}
.search-bar {
    display: flex; align-items: center; gap: 0.5rem;
    background: var(--bg-surface);
    padding: 0.5rem 1rem;
    border-radius: 8px;
    width: 400px;
    color: var(--text-muted);
}
.search-bar input {
    background: transparent; border: none; outline: none;
    font-family: inherit; font-size: 0.875rem; width: 100%;
    color: var(--text-main);
}
.top-actions {
    display: flex; align-items: center; gap: 0.75rem;
}

/* BUTTONS */
.btn {
    display: flex; align-items: center; justify-content: center; gap: 0.5rem;
    padding: 0.5rem 1rem; border: none; border-radius: 8px;
    font-family: 'Inter', sans-serif; font-size: 0.875rem; font-weight: 500;
    cursor: pointer; transition: 0.2s; outline: none;
}
.btn:disabled { opacity: 0.6; cursor: not-allowed; }
.btn.primary { background: var(--primary); color: white; }
.btn.primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn.secondary { background: var(--bg-surface); color: var(--text-main); border: 1px solid var(--border); }
.btn.secondary:hover:not(:disabled) { background: var(--border); }
.btn.outline { background: transparent; border: 1px solid var(--border); color: var(--text-main); }
.btn.outline:hover:not(:disabled) { background: var(--bg-surface); }
.btn.icon-btn { padding: 0.4rem; background: transparent; color: var(--text-muted); }
.btn.icon-btn:hover:not(:disabled) { background: var(--bg-surface); color: var(--text-main); }
.btn.sub-action { background: var(--bg-surface); color: var(--text-main); }
.dropdown-btn { padding-right: 0.5rem; }

/* VIEW CONTAINER */
.view-container {
    flex: 1; overflow: auto;
    position: relative;
    background: var(--bg-white);
    display: flex;
    flex-direction: column;
}

/* HOME VIEW */
.home-view {
    padding: 1.5rem 3rem 3rem 3rem;
    max-width: 1200px;
    width: 100%;
    margin: 0 auto;
    display: flex; flex-direction: column; gap: 1rem;
    flex: 1;
}
.home-header {
    display: flex; justify-content: space-between; align-items: flex-end;
}
.overline {
    font-size: 0.7rem; font-weight: 600; color: var(--text-sub); letter-spacing: 0.05em;
}
.home-title-area h1 { margin: 0; font-size: 2rem; font-weight: 700; }
.home-title-area p { margin: 0; color: var(--text-muted); font-size: 0.95rem; }
.home-badge {
    background: var(--bg-surface);
    padding: 0.5rem 1rem;
    border-radius: 20px;
    font-size: 0.8rem; color: var(--text-muted);
}

.table-card {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 1.5rem;
    flex: 1;
}
.table-card-header {
    display: flex; justify-content: space-between; align-items: flex-start;
    margin-bottom: 1.5rem;
}
.table-card-titles h3 { margin: 0 0 0.25rem 0; font-size: 0.8rem; font-weight: 600; letter-spacing: 0.05em; color: var(--text-sub); text-transform: uppercase; }
.table-card-titles p { margin: 0; font-size: 0.85rem; color: var(--text-muted); }
.table-card-actions { display: flex; align-items: center; gap: 1rem; }
.count-badge { background: white; padding: 0.25rem 0.75rem; border-radius: 12px; font-size: 0.8rem; border: 1px solid var(--border); }

.styled-table { width: 100%; border-collapse: collapse; }
.styled-table th {
    text-align: left; padding: 1rem 0.5rem;
    font-size: 0.7rem; font-weight: 700; color: var(--text-sub);
    letter-spacing: 0.05em; border-bottom: 2px solid var(--border);
}
.styled-table td {
    padding: 1rem 0.5rem; border-bottom: 1px solid var(--border);
    vertical-align: middle;
}
.styled-table tr:hover td { background: var(--bg-white); }
.styled-table tr:last-child td { border-bottom: none; }

.template-item { display: flex; align-items: center; gap: 1rem; }
.template-icon {
    width: 36px; height: 36px; border-radius: 8px;
    display: flex; justify-content: center; align-items: center;
}
.template-info { display: flex; flex-direction: column; gap: 0.25rem; }
.t-name { font-weight: 600; font-size: 0.95rem; }
.t-sub { font-size: 0.75rem; color: var(--text-sub); }
.doc-badge { background: var(--bg-white); padding: 0.25rem 0.5rem; border-radius: 12px; font-size: 0.75rem; border: 1px solid var(--border); color: var(--text-muted); }
.action-dropdown {
    position: relative;
    display: inline-flex; overflow: visible; border-radius: 6px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}
.action-dropdown .view-btn { padding: 0.4rem 0.75rem; background: var(--primary); color: white; border: none; cursor: pointer; border-right: 1px solid rgba(255,255,255,0.2); font-size: 0.8rem; font-weight: 500; font-family: inherit;}
.action-dropdown .drop-btn { padding: 0.4rem; background: var(--primary); color: white; border: none; cursor: pointer; }
.action-menu {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    min-width: 160px;
    background: white;
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 14px 32px rgba(15, 23, 42, 0.14);
    padding: 0.35rem;
    z-index: 20;
}
.action-menu-item {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 0.6rem;
    background: transparent;
    border: none;
    border-radius: 8px;
    padding: 0.6rem 0.75rem;
    color: var(--text-main);
    font-size: 0.82rem;
    font-weight: 500;
    cursor: pointer;
    font-family: inherit;
}
.action-menu-item:hover { background: var(--bg-surface); }
.action-menu-item.danger { color: var(--danger); }
.api-doc-card { padding-bottom: 1.25rem; }
.api-doc-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1rem;
    padding: 0 1.5rem;
}
.api-doc-section {
    border: 1px solid var(--border);
    border-radius: 14px;
    background: white;
    overflow: hidden;
}
.api-doc-section-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 1rem 0.75rem 1rem;
}
.api-doc-section-head h3 {
    margin: 0;
    font-size: 0.95rem;
    color: var(--text-main);
}
.mini-copy {
    width: auto;
    padding: 0.4rem 0.7rem;
    font-size: 0.75rem;
}
.api-code-block {
    margin: 0;
    padding: 1rem;
    background: #0F172A;
    color: #E2E8F0;
    overflow: auto;
    font-size: 0.82rem;
    line-height: 1.5;
    white-space: pre-wrap;
    word-break: break-word;
}


/* EDITOR VIEW */
.editor-view {
    display: flex; flex-direction: column;
    height: 100%;
}
.editor-top-bar {
    padding: 0.35rem 1.5rem;
    border-bottom: 1px solid var(--border);
    display: flex; align-items: center; background: white;
}
.header-title-wrap {
    display: flex; align-items: center; gap: 0.75rem;
}
.header-title-wrap h2 { margin: 0; font-size: 1.15rem; }
.header-title-input {
    background: transparent; border: 1px solid var(--border); padding: 0.25rem 0.5rem; border-radius: 4px; font-size: 1.1rem; font-weight: 600; outline: none; width: 300px;
}
.editor-main-layout {
    display: flex; flex: 1; overflow: hidden; height: 100%;
}

.inner-sidebar {
    width: 300px;
    border-right: 1px solid var(--border);
    background: var(--bg-white);
    display: flex; flex-direction: column;
    box-shadow: none; /* remove legacy dark shadow */
}

/* Upload & Fields inside sidebar */
.upload-zone { padding: 1.5rem; border-bottom: 1px solid var(--border); }
.fields-section { flex: 1; overflow-y: auto; padding: 1.5rem; display: flex; flex-direction: column; }
.fields-section h3 { margin: 0 0 1rem 0; font-size: 0.75rem; text-transform: uppercase; color: var(--text-sub); letter-spacing: 0.05em; font-weight: 600;}
.fields-list { display: flex; flex-direction: column; gap: 0.75rem; }
.empty-state { font-size: 0.875rem; color: var(--text-muted); text-align: center; margin-top: 2rem; }

.variable-card {
    background: white; border: 1px solid var(--border); border-radius: 8px; padding: 0.75rem;
    display: flex; flex-direction: column; gap: 0.5rem; box-shadow: 0 1px 2px rgba(0,0,0,0.02);
}
.variable-card.selected { border-color: var(--primary); box-shadow: 0 0 0 2px var(--primary-light); }
.variable-card input {
    background: var(--bg-surface); border: none; color: var(--text-main);
    padding: 0.5rem; border-radius: 6px; font-size: 0.85rem; font-weight: 500;
    font-family: inherit; width: 100%; outline: none;
}
.card-actions { display: flex; justify-content: space-between; align-items: center; }
.card-tools { display: flex; gap: 0.25rem; }
.tool-btn, .delete-btn { background: none; border: none; font-size: 0.7rem; font-weight: 500; cursor: pointer; color: var(--text-muted); padding: 4px; }
.tool-btn:hover { color: var(--primary); }
.tool-btn.active { color: var(--primary); font-weight: 700; }
.delete-btn { color: var(--danger); }
.delete-btn:hover { background: #FEF2F2; border-radius: 4px; }

.action-panel { padding: 1.5rem; border-top: 1px solid var(--border); background: var(--bg-surface); }

/* WORKSPACE */
.workspace {
    flex: 1; display: flex; flex-direction: column; background: var(--bg-app); position: relative;
    border-radius: 0 0 16px 0; /* Match outer corner */
}
.toolbar {
    height: 48px; border-bottom: 1px solid var(--border);
    display: flex; align-items: center; justify-content: space-between; padding: 0 1rem;
    background: var(--bg-white);
}
.tool-group { display: flex; align-items: center; gap: 0.5rem; font-size: 0.8rem; color: var(--text-muted); font-weight: 500; }
.btn.mini { padding: 0.25rem 0.5rem; }

.canvas-wrapper {
    flex: 1; overflow: auto; padding: 2rem; display: flex; justify-content: center; align-items: flex-start;
}
.placeholder { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100%; color: var(--text-muted); gap: 1rem; font-size: 0.9rem;}

.pdf-container { position: relative; box-shadow: 0 8px 30px rgba(0,0,0,0.1); background: white; border-radius: 4px; overflow: hidden; }
#drawLayer { position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 5; }

/* SELECTION BOXES */
.selection-box { position: absolute; border: 2px solid var(--box-border); background-color: var(--box-bg); pointer-events: none; }
.selection-box.final { pointer-events: auto; cursor: pointer; }
.selection-box.final:hover { background-color: rgba(79, 70, 229, 0.25); }
.selection-box.selected { border-color: var(--primary); box-shadow: 0 0 10px rgba(79, 70, 229, 0.4); z-index: 10;}
.selection-box.move-active { border-color: #F59E0B; background-color: rgba(245, 158, 11, 0.2); z-index: 11; }

.selection-label {
    background: var(--primary); color: white; padding: 2px 6px; font-size: 10px; font-weight: 600;
    white-space: nowrap; box-shadow: 0 2px 4px rgba(0,0,0,0.1); font-family: 'Inter', sans-serif;
}
.selection-box.move-active .selection-label { background: #F59E0B; }

/* RESULTS SIDEBAR */
.right-sidebar {
    width: 320px; border-left: 1px solid var(--border); border-right: none;
    background: var(--bg-white); transform: translateX(100%); transition: transform 0.3s ease;
    position: absolute; right: 0; top: 0; height: 100%; display: flex; flex-direction: column; z-index: 20;
    border-radius: 0 0 16px 0;
}
.right-sidebar.hidden { transform: translateX(100%); display: flex !important; }
.right-sidebar:not(.hidden) { transform: translateX(0); }
.result-content { flex: 1; overflow-y: auto; padding: 1.5rem; background: var(--bg-surface); }
.result-content pre { margin: 0; padding: 1rem; border-radius: 8px; color: var(--primary); font-size: 0.8rem; line-height: 1.4; background: white; white-space: pre-wrap; font-family: monospace; border: 1px solid var(--border); box-shadow: 0 1px 2px rgba(0,0,0,0.05); }

#loadingIndicator { text-align: center; margin-top: 0.75rem; font-size: 0.75rem; color: var(--primary); font-weight: 500; }
