:root {
    --bg: #f7f8fb;
    --card: #ffffff;
    --border: #dcdfe6;
    --primary: #0b6efd;
    --primary-dark: #0a58ca;
    --accent: #ffb74d;
    --text: #1f2a37;
    --danger: #d14343;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
}

.app {
    padding: 24px;
    max-width: 100%;
}

.app__header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 16px;
    margin-bottom: 16px;
}

.app__header h1 {
    margin: 0 0 4px;
    font-size: 24px;
}

.app__header p {
    margin: 0;
    color: #6b7280;
}

.actions {
    position: relative;
}

.actions button,
.search-panel button {
    padding: 8px 16px;
    border: none;
    border-radius: 6px;
    background: var(--primary);
    color: #fff;
    font-weight: 600;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    transition: background 0.2s ease, transform 0.2s ease;
}

button.secondary {
    background: #fff;
    color: var(--text);
    border: 1px solid var(--border);
}

button.primary-btn:hover {
    background: var(--primary-dark);
}

button.primary-btn .icon {
    font-size: 18px;
}

.search-panel {
    display: flex;
    gap: 8px;
    align-items: center;
    margin-bottom: 16px;
}

.search-panel input {
    flex: 1;
    padding: 8px;
    border: 1px solid var(--border);
    border-radius: 6px;
}

.table-wrapper {
    overflow-x: auto;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--card);
    position: relative;
    box-shadow: 0 20px 45px rgba(15, 23, 42, 0.08);
}

#registry-table {
    width: 100%;
    border-collapse: separate; /* Changed to separate to allow border-spacing if needed, but collapse is usually fine. Sticking to collapse but adding fixed layout */
    border-collapse: collapse;
    table-layout: fixed; /* CRITICAL for column resizing */
    min-width: 1400px;
}

#registry-table thead th {
    background: #f0f4ff;
    border: 1px solid var(--border);
    padding: 12px 8px;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    position: relative; /* Ensure resizers are positioned relative to th */
    overflow: visible;
}

#registry-table tbody tr {
    position: relative;
    min-height: 62px;
}

#registry-table tbody td {
    border: 1px solid var(--border);
    padding: 0;
    vertical-align: top;
    position: relative;
    overflow: visible;
}

.field-wrapper {
    position: relative;
    height: 100%;
    padding: 0;
    display: flex;
    flex-direction: column;
}

.table-input {
    width: 100%;
    flex: 1 1 auto;
    min-height: 100%;
    height: auto;
    padding: 10px 12px;
    border: 1px solid transparent;
    border-radius: 0;
    background: transparent;
    font-size: 14px;
    color: var(--text);
    transition: border 0.2s ease, background 0.2s ease, box-shadow 0.2s ease;
    font-family: inherit;
}

.table-input::placeholder {
    color: #9ca3af;
}

.table-input:focus {
    outline: none;
    border-color: var(--primary);
    background: #fff;
    box-shadow: 0 10px 25px rgba(15, 23, 42, 0.08);
}

.table-textarea {
    resize: none;
    min-height: 38px;
    height: auto !important;
    max-height: 300px;
    overflow-y: auto;
    line-height: 1.45;
    display: block;
    padding: 10px 12px;
}

.table-textarea::-webkit-scrollbar {
    width: 6px;
    display: block; /* Show scrollbar */
}

.table-textarea::-webkit-scrollbar-track {
    background: transparent;
}

.table-textarea::-webkit-scrollbar-thumb {
    background-color: rgba(156, 163, 175, 0.5);
    border-radius: 3px;
}

.field-wrapper.has-file {
    box-shadow: inset 0 0 0 2px rgba(255, 183, 77, 0.85);
}

.field-wrapper.has-file::after {
    content: 'Файл';
    position: absolute;
    top: 8px;
    right: 10px;
    font-size: 11px;
    padding: 2px 6px;
    border-radius: 4px;
    background: rgba(255, 183, 77, 0.2);
    color: #b56a00;
}

.registry-cell {
    position: relative;
}

.entity-badge {
    position: absolute;
    top: 8px;
    left: 8px;
    padding: 2px 6px;
    border-radius: 12px;
    font-size: 11px;
    text-transform: uppercase;
    background: #eef2ff;
    color: #4c51bf;
    pointer-events: none;
}

.entity-badge.entity-badge--hidden {
    display: none;
}

.table-input.with-badge {
    padding-left: 70px;
}

.row-select-area {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 24px; /* Wider hit area for easier clicking */
    background: transparent;
    cursor: pointer;
    transition: background 0.2s ease;
    z-index: 5; /* Above input */
}

.row-select-area::after {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 4px; /* Visible thin strip */
    background: transparent;
    border-radius: 0 4px 4px 0;
    transition: background 0.2s ease;
}

.row-select-area:hover::after {
    background: rgba(11, 110, 253, 0.3);
}

tr.row-selected .row-select-area::after {
    background: var(--primary);
}

tr.row-selected td {
    background: #eef4ff;
}

.row-height-handle {
    position: absolute;
    left: 0; /* Full width of cell */
    right: 0;
    bottom: -6px; /* Slightly lower to straddle the line */
    height: 12px; /* Larger hit area */
    cursor: row-resize;
    opacity: 0;
    transition: opacity 0.2s ease;
    z-index: 20; /* High z-index */
}

.row-height-handle::after {
    content: '';
    position: absolute;
    left: 0;
    right: 0;
    top: 3px;
    height: 2px;
    border-radius: 2px;
    background: rgba(148, 163, 184, 0.7);
}

tr:hover .row-height-handle {
    opacity: 1;
}

.document-results {
    margin-top: 24px;
    padding: 16px;
    background: var(--card);
    border-radius: 12px;
    border: 1px solid var(--border);
}

.document-results ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

.document-results li {
    padding: 12px 0;
    border-bottom: 1px solid var(--border);
}

.document-results li:last-child {
    border-bottom: none;
}

.document-results .doc-title {
    font-weight: 600;
}

.document-results .doc-meta {
    font-size: 13px;
    color: #667085;
}

.document-results .doc-excerpt {
    margin-top: 4px;
    font-style: italic;
    color: #475467;
}

.document-results .doc-actions {
    margin-top: 8px;
    font-size: 14px;
}

.document-results .doc-actions a {
    color: var(--primary);
    text-decoration: none;
    margin-right: 8px;
}

.context-menu {
    position: fixed;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 20px 45px rgba(15, 23, 42, 0.15);
    padding: 8px 0;
    z-index: 1000;
    display: flex;
    flex-direction: column;
    min-width: 180px;
}

.context-menu.hidden {
    display: none;
}

.context-section {
    padding: 6px 0;
}

.context-title {
    font-size: 11px;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: #94a3b8;
    padding: 4px 16px;
}

.context-divider {
    border-top: 1px solid var(--border);
    margin: 0 12px;
}

.context-menu button {
    text-align: left;
    background: transparent;
    border: none;
    width: 100%;
    padding: 8px 16px;
    cursor: pointer;
    color: var(--text);
    font-size: 14px;
    transition: background 0.15s ease;
}

.context-menu button:hover:not(:disabled) {
    background: #f1f5f9;
    color: var(--primary);
}

.context-menu button.danger {
    color: var(--danger);
}

.context-menu button:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
}

.status-panel {
    margin-bottom: 16px;
    padding: 8px 12px;
    border-radius: 6px;
    background: #fff8e1;
    border: 1px solid #ffe082;
    color: #8d6e63;
}

.status-panel.status-success {
    background: #e8f5e9;
    border-color: #a5d6a7;
    color: #2e7d32;
}

.status-panel.status-error {
    background: #ffebee;
    border-color: #ef9a9a;
    color: #c62828;
}

.status-panel.status-warning {
    background: #fff8e1;
    border-color: #ffe082;
    color: #8d6e63;
}

.status-panel.status-info {
    background: #e3f2fd;
    border-color: #90caf9;
    color: #1565c0;
}

.bulk-actions {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 10px 16px;
    margin-bottom: 16px;
    box-shadow: 0 25px 50px rgba(15, 23, 42, 0.08);
}

.bulk-actions.hidden {
    display: none;
}

.bulk-info {
    font-weight: 600;
    color: #1f2937;
}

.bulk-info span {
    color: var(--primary);
}

.bulk-buttons {
    display: flex;
    gap: 8px;
}

.bulk-buttons .danger {
    background: var(--danger);
    border: none;
    color: #fff;
}

.pagination {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 10px 16px;
    margin: 20px 0;
    box-shadow: 0 20px 45px rgba(15, 23, 42, 0.08);
}

.pagination.hidden {
    display: none;
}

.pagination__controls {
    display: flex;
    align-items: center;
    gap: 8px;
}

.pagination__display {
    font-weight: 600;
    color: #1f2937;
}

.pagination__size {
    display: flex;
    align-items: center;
    gap: 8px;
    color: #475467;
    font-weight: 500;
}

.pagination__size select {
    padding: 4px 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: #fff;
}

body.is-resizing-row {
    cursor: row-resize;
    user-select: none;
}

/* Login Overlay */
.login-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.95);
    z-index: 2000;
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(5px);
}

.login-overlay.hidden {
    display: none;
}

.login-box {
    background: #fff;
    padding: 32px;
    border-radius: 12px;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.1);
    width: 100%;
    max-width: 360px;
    border: 1px solid var(--border);
}

.login-box h2 {
    margin-top: 0;
    margin-bottom: 24px;
    text-align: center;
    color: var(--text);
}

.login-box .form-group {
    margin-bottom: 16px;
}

.login-box label {
    display: block;
    margin-bottom: 6px;
    font-size: 14px;
    font-weight: 500;
    color: #4b5563;
}

.login-box input {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 14px;
    transition: border-color 0.2s;
}

.login-box input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(11, 110, 253, 0.1);
}

.login-box .primary-btn {
    width: 100%;
    justify-content: center;
    margin-top: 8px;
}

.error-message {
    margin-top: 16px;
    padding: 10px;
    background: #fee2e2;
    border: 1px solid #fecaca;
    color: #b91c1c;
    border-radius: 6px;
    font-size: 13px;
    text-align: center;
}

.error-message.hidden {
    display: none;
}

.app.blurred {
    filter: blur(4px);
    pointer-events: none;
}

.add-menu {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 10px;
    min-width: 240px;
    box-shadow: 0 25px 50px rgba(15, 23, 42, 0.2);
    padding: 8px 0;
    animation: fadeIn 0.2s ease;
    z-index: 1000; /* Increased z-index */
}

.add-menu.hidden {
    display: none;
}

.add-menu__title {
    font-size: 12px;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: #94a3b8;
    padding: 4px 16px 8px;
}

.add-menu button {
    width: 100%;
    background: #fff; /* Explicit background */
    border: none;
    text-align: left;
    padding: 8px 16px;
    font-size: 14px;
    cursor: pointer;
    color: var(--text); /* Explicit text color */
    display: block; /* Ensure block display */
}

.add-menu button:hover {
    background: #f1f5f9;
    color: var(--primary);
}

.empty-cell {
    text-align: center;
    padding: 24px 0;
    color: #94a3b8;
}

/* Column resizing */
.resize-row {
    height: 0;
}

.resize-row th {
    border: none !important;
    padding: 0 !important;
    height: 0 !important;
    position: relative;
}

.col-resizer {
    display: block;
    position: absolute;
    right: -6px;
    bottom: 0;
    width: 13px;
    top: 0;
    height: auto;
    cursor: col-resize;
    z-index: 100;
    pointer-events: auto;
}

.col-resizer::before {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 100px;
    z-index: 100;
}

.col-resizer::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 5px;
    width: 3px;
    height: 100px;
    background: rgba(148, 163, 184, 0.4);
    border-radius: 1px;
    transition: background 0.2s ease, width 0.2s ease;
    z-index: 101;
}

.col-resizer:hover::after,
.col-resizer.active::after {
    background: var(--primary);
    width: 5px;
    left: 4px;
    opacity: 1;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(-6px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}
