:root {
    --bg: #f5f6f8;
    --panel: #fff;
    --text: #1f2933;
    --text-light: #7b8794;
    --border: #d9dee4;
    --primary: #2563eb;
    --danger: #dc2626;
    --success: #16a34a;
}

* { box-sizing: border-box; }
body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
}

.topbar {
    display: flex; align-items: center; justify-content: space-between;
    padding: 10px 20px; background: var(--panel); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 600; font-size: 18px; }
.nav-btn {
    background: none; border: none; padding: 8px 14px; font-size: 15px;
    color: var(--text-light); cursor: pointer; border-radius: 6px;
}
.nav-btn.active { color: var(--primary); background: #eef3ff; }

main { max-width: 900px; margin: 20px auto; padding: 0 16px; }
.view { display: none; }
.view.active { display: block; }
.placeholder { padding: 40px; text-align: center; color: var(--text-light); }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 12px; }
.tab {
    background: none; border: none; padding: 10px 16px; cursor: pointer;
    font-size: 14px; color: var(--text-light); border-bottom: 2px solid transparent;
}
.tab.active { color: var(--primary); border-bottom-color: var(--primary); }

.show-hidden { display: inline-block; margin-bottom: 10px; font-size: 13px; color: var(--text-light); }

.grid { width: 100%; border-collapse: collapse; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; }
.grid th { text-align: left; font-size: 12px; text-transform: uppercase; color: var(--text-light); padding: 8px 10px; border-bottom: 1px solid var(--border); }
.grid td { padding: 6px 10px; border-bottom: 1px solid #eef0f3; }
.grid input:not([type=checkbox]) {
    width: 100%; padding: 6px 8px; border: 1px solid transparent; border-radius: 4px; font-size: 14px; background: transparent;
}
.grid input:not([type=checkbox]):hover { border-color: var(--border); }
.grid input:not([type=checkbox]):focus { border-color: var(--primary); outline: none; background: #fff; }
.grid input:disabled { color: var(--text-light); }
.grid input.narrow { width: 70px; }
.grid .num { text-align: right; color: var(--text-light); font-size: 13px; }
.grid .empty { text-align: center; color: var(--text-light); padding: 20px; }
.hidden-row td { opacity: 0.5; }
.add-row td { background: #fafbfc; border-top: 1px solid var(--border); }
.add-row input:not([type=checkbox]) { border-color: var(--border) !important; background: #fff !important; }

.btn-primary { background: var(--primary); color: #fff; border: none; padding: 6px 16px; border-radius: 4px; cursor: pointer; }
.btn-del { background: none; border: none; color: var(--danger); cursor: pointer; font-size: 13px; }

.toast {
    position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(80px);
    background: #333; color: #fff; padding: 10px 18px; border-radius: 6px; font-size: 14px;
    transition: transform .2s; z-index: 9999;
}
.toast.show { transform: translateX(-50%) translateY(0); }
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }

/* ===== Step 2: documents ===== */
main { max-width: 1100px; }

.btn { background: #fff; color: var(--text); border: 1px solid var(--border); padding: 6px 14px; border-radius: 4px; cursor: pointer; font-size: 14px; }
.btn:hover { background: #f0f2f5; }
.btn-primary { font-size: 14px; }
.btn-danger { background: none; border: 1px solid var(--danger); color: var(--danger); padding: 6px 14px; border-radius: 4px; cursor: pointer; font-size: 14px; }

.toolbar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }

.drop-zone {
    border: 2px dashed var(--border); border-radius: 8px; padding: 14px; text-align: center;
    color: var(--text-light); font-size: 14px; margin-bottom: 12px; transition: all .15s;
}
.drop-zone.active { border-color: var(--primary); background: #eef3ff; color: var(--primary); }

.doc-grid td { padding: 9px 10px; font-size: 14px; }
.doc-grid tr.clickable { cursor: pointer; }
.doc-grid tr.clickable:hover td { background: #f7f9fc; }

/* Modal */
.modal-overlay[hidden] { display: none; }
.modal-overlay {
    position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 1000;
    display: flex; align-items: center; justify-content: center; padding: 16px;
}
.modal {
    background: var(--panel); border-radius: 10px; width: 100%; max-width: 1100px; height: 90vh;
    display: flex; flex-direction: column; box-shadow: 0 10px 40px rgba(0,0,0,.3);
}
.modal-head { display: flex; align-items: center; gap: 12px; padding: 12px 16px; border-bottom: 1px solid var(--border); font-weight: 600; }
.modal-head .counter { color: var(--text-light); font-weight: 400; font-size: 14px; }
.btn-x { margin-left: auto; background: none; border: none; font-size: 18px; cursor: pointer; color: var(--text-light); }
.modal-body { flex: 1; display: flex; min-height: 0; }

.preview { flex: 1; background: #2b2f36; display: flex; align-items: center; justify-content: center; min-width: 0; border-bottom-left-radius: 10px; }
.preview iframe { width: 100%; height: 100%; border: 0; background: #fff; }
.preview img { max-width: 100%; max-height: 100%; object-fit: contain; }
.preview-msg { color: #e5e7eb; text-align: center; padding: 20px; font-size: 15px; }
.preview-msg a { color: #93c5fd; }
.preview-msg .sub { margin-top: 8px; font-size: 13px; color: #9ca3af; word-break: break-all; }

.doc-form { width: 340px; padding: 16px; overflow-y: auto; display: flex; flex-direction: column; gap: 10px; }
.doc-form label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-light); text-transform: uppercase; }
.doc-form label[hidden] { display: none; }
.doc-form input, .doc-form select, .doc-form textarea {
    font: inherit; font-size: 14px; text-transform: none; color: var(--text);
    padding: 7px 9px; border: 1px solid var(--border); border-radius: 4px; background: #fff;
}
.doc-form input:focus, .doc-form select:focus, .doc-form textarea:focus { outline: none; border-color: var(--primary); }
.doc-form .row2 { display: flex; gap: 8px; }
.doc-form .row2 label { flex: 1; min-width: 0; }
.form-buttons { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 6px; }

/* Phone: stack preview above the form */
@media (max-width: 760px) {
    .grid.doc-grid th:nth-child(3), .grid.doc-grid td:nth-child(3),
    .grid.doc-grid th:nth-child(7), .grid.doc-grid td:nth-child(7) { display: none; }
    .modal { height: 95vh; }
    .modal-body { flex-direction: column; }
    .preview { flex: 0 0 40%; border-radius: 0; }
    .doc-form { width: 100%; }
}

/* ===== Office / text previews ===== */
.preview-doc { align-self: stretch; width: 100%; height: 100%; overflow: auto; background: #fff; color: var(--text); display: flex; flex-direction: column; }
.sheet-tabs { display: flex; gap: 2px; padding: 6px 6px 0; background: #f0f2f5; border-bottom: 1px solid var(--border); position: sticky; top: 0; flex-wrap: wrap; }
.sheet-tab { border: 1px solid var(--border); border-bottom: none; background: #e5e7eb; padding: 5px 12px; font-size: 13px; cursor: pointer; border-radius: 4px 4px 0 0; }
.sheet-tab.active { background: #fff; color: var(--primary); }
.sheet-body { flex: 1; }
table.sheet { border-collapse: collapse; font-size: 12px; }
table.sheet td { border: 1px solid #e1e4e8; padding: 3px 6px; white-space: nowrap; max-width: 320px; overflow: hidden; text-overflow: ellipsis; }
table.sheet tr:first-child td { background: #f6f8fa; font-weight: 600; }
.sheet-note { padding: 10px; color: var(--text-light); font-size: 13px; }
.docx-host { background: #e5e7eb; }
.text-preview { margin: 0; padding: 14px; font-size: 13px; white-space: pre-wrap; word-break: break-word; }

/* ===== Filter panel + search ===== */
main { max-width: 1300px; }

.search-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 14px; margin-bottom: 12px; font-size: 13px; color: var(--text-light); }
.search-bar input { font: inherit; font-size: 14px; color: var(--text); padding: 6px 8px; border: 1px solid var(--border); border-radius: 4px; background: #fff; }
.search-bar input:focus { outline: none; border-color: var(--primary); }
.search-bar .s-keyword { flex: 1; min-width: 220px; }
.search-bar .s-amt { width: 90px; }
.btn-link { background: none; border: none; color: var(--primary); cursor: pointer; font-size: 13px; padding: 4px; }

.doc-layout { display: flex; gap: 16px; align-items: flex-start; }
.doc-main { flex: 1; min-width: 0; }
.result-info { font-size: 13px; color: var(--text-light); margin: 0 0 6px 2px; }
.result-info a { color: var(--primary); }

.filter-panel { width: 230px; flex-shrink: 0; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 6px 0; position: sticky; top: 12px; max-height: calc(100vh - 24px); overflow-y: auto; }
.fp-section { padding: 6px 0 10px; border-bottom: 1px solid #eef0f3; }
.fp-section:last-child { border-bottom: none; }
.fp-head { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--text-light); padding: 4px 14px 6px; font-weight: 600; }
.fp-item { display: flex; align-items: center; gap: 4px; padding: 5px 10px 5px 14px; font-size: 14px; cursor: pointer; }
.fp-item:hover { background: #f3f5f8; }
.fp-item.selected { background: #e8efff; color: var(--primary); font-weight: 600; }
.fp-item.is-hidden .fp-name { color: var(--text-light); font-style: italic; }
.fp-item.fp-none .fp-name { color: var(--text-light); }
.fp-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fp-count { font-size: 12px; color: var(--text-light); min-width: 18px; text-align: right; }
.fp-actions { display: none; gap: 2px; }
.fp-item:hover .fp-actions { display: flex; }
.fp-actions button { background: none; border: none; cursor: pointer; font-size: 13px; padding: 0 3px; opacity: .65; }
.fp-actions button:hover { opacity: 1; }
.fp-item.editing { cursor: default; background: none; }
.fp-input { width: 100%; font: inherit; font-size: 14px; padding: 4px 6px; border: 1px solid var(--primary); border-radius: 4px; }
.fp-add, .fp-hidden-toggle { display: block; background: none; border: none; color: var(--primary); cursor: pointer; font-size: 13px; padding: 5px 14px; text-align: left; }
.fp-hidden-toggle { color: var(--text-light); font-size: 12px; }
.fp-check { display: flex; align-items: center; gap: 8px; padding: 4px 10px 4px 14px; font-size: 14px; cursor: pointer; }
.fp-check:hover { background: #f3f5f8; }
.fp-empty { padding: 4px 14px; font-size: 13px; color: var(--text-light); }

.filters-toggle { display: none; }

/* Type-to-search fields in the document form */
.combo { position: relative; }
.combo input { width: 100%; }
.combo-list { position: absolute; left: 0; right: 0; top: 100%; margin-top: 2px; background: #fff; border: 1px solid var(--border); border-radius: 4px; box-shadow: 0 6px 18px rgba(0,0,0,.12); max-height: 220px; overflow-y: auto; z-index: 20; text-transform: none; }
.combo-opt { padding: 7px 10px; font-size: 14px; color: var(--text); cursor: pointer; }
.combo-opt:hover, .combo-opt.first { background: #eef3ff; }
.combo-add { color: var(--primary); border-top: 1px solid #eef0f3; }

@media (max-width: 760px) {
    .filters-toggle { display: inline-block; }
    .doc-layout { display: block; }
    .filter-panel { display: none; position: static; width: 100%; max-height: none; margin-bottom: 12px; }
    .filter-panel.open { display: block; }
    .fp-actions { display: flex; }   /* no hover on phones */
}

/* ===== Sign-in ===== */
.topbar nav { margin-left: auto; margin-right: 16px; }
.user-box { display: flex; align-items: center; gap: 12px; font-size: 14px; color: var(--text-light); }
.user-box a { color: var(--primary); text-decoration: none; }

.login-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 36px 32px; width: 100%; max-width: 380px; text-align: center; box-shadow: 0 8px 30px rgba(0,0,0,.06); }
.login-card h1 { margin: 0 0 6px; font-size: 24px; }
.login-sub { margin: 0 0 24px; color: var(--text-light); font-size: 14px; }
.login-error { background: #fef2f2; color: var(--danger); border: 1px solid #fecaca; border-radius: 6px; padding: 10px 12px; font-size: 14px; margin-bottom: 18px; text-align: left; }
.google-btn { display: inline-flex; align-items: center; justify-content: center; gap: 10px; width: 100%; padding: 11px 16px; border: 1px solid var(--border); border-radius: 6px; background: #fff; color: var(--text); font-size: 15px; font-weight: 500; text-decoration: none; }
.google-btn:hover { background: #f7f8fa; }

@media (max-width: 760px) {
    .user-box span { display: none; }
}
.user-box[hidden] { display: none; }
