*,
*::before,
*::after {
    box-sizing: border-box;
}

:root {
    --bg: #f4f6f9;
    --surface: #ffffff;
    --border: #d8dee9;
    --text: #1e293b;
    --muted: #64748b;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --success: #059669;
    --success-hover: #047857;
    --danger: #dc2626;
    --danger-hover: #b91c1c;
    --field-text: #2563eb;
    --field-textbox: #7c3aed;
    --field-checkbox: #059669;
    --field-radio: #d97706;
    --field-signature: #db2777;
    --shadow: 0 1px 3px rgba(15, 23, 42, 0.08);
}

body {
    margin: 0;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    font-size: 14px;
    color: var(--text);
    background: var(--bg);
    min-height: 100vh;
}

.app-header {
    padding: 1rem 1.5rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow);
}

.app-header h1 {
    margin: 0 0 0.25rem;
    font-size: 1.35rem;
    font-weight: 600;
}

.subtitle {
    margin: 0;
    color: var(--muted);
    font-size: 0.9rem;
}

.app-main {
    display: grid;
    grid-template-columns: 240px 1fr 280px;
    gap: 0;
    min-height: calc(100vh - 80px);
}

.toolbar,
.properties {
    background: var(--surface);
    border-right: 1px solid var(--border);
    padding: 1rem;
    overflow-y: auto;
}

.properties {
    border-right: none;
    border-left: 1px solid var(--border);
}

.toolbar-section {
    margin-bottom: 1.25rem;
}

.toolbar-section h2,
.properties h2 {
    margin: 0 0 0.6rem;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--muted);
}

.hint {
    margin: 0 0 0.5rem;
    font-size: 0.8rem;
    color: var(--muted);
}

.btn {
    display: inline-block;
    padding: 0.45rem 0.75rem;
    font-size: 0.85rem;
    font-family: inherit;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}

.btn:hover:not(:disabled) {
    background: var(--bg);
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-primary {
    background: var(--primary);
    border-color: var(--primary);
    color: #fff;
    width: 100%;
}

.btn-primary:hover:not(:disabled) {
    background: var(--primary-hover);
}

.btn-success {
    background: var(--success);
    border-color: var(--success);
    color: #fff;
    width: 100%;
}

.btn-success:hover:not(:disabled) {
    background: var(--success-hover);
}

.btn-danger {
    background: var(--danger);
    border-color: var(--danger);
    color: #fff;
    width: 100%;
    margin-top: 0.5rem;
}

.btn-danger:hover:not(:disabled) {
    background: var(--danger-hover);
}

.tool-buttons {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}

.btn-tool {
    width: 100%;
    text-align: left;
}

.btn-tool.active {
    background: #eff6ff;
    border-color: var(--primary);
    color: var(--primary);
    font-weight: 500;
}

.page-nav {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.page-nav .btn {
    flex: 1;
    padding: 0.35rem 0.5rem;
}

#page-indicator {
    font-size: 0.85rem;
    color: var(--muted);
    min-width: 4rem;
    text-align: center;
}

.field-list {
    list-style: none;
    margin: 0;
    padding: 0;
    max-height: 200px;
    overflow-y: auto;
}

.field-list li {
    padding: 0.4rem 0.5rem;
    border-radius: 4px;
    font-size: 0.8rem;
    cursor: pointer;
    border: 1px solid transparent;
}

.field-list li:hover {
    background: var(--bg);
}

.field-list li.selected {
    background: #eff6ff;
    border-color: #bfdbfe;
}

.field-list-empty {
    color: var(--muted);
    cursor: default;
}

.field-list-empty:hover {
    background: transparent;
}

.viewer-wrap {
    position: relative;
    overflow: auto;
    background: #94a3b8;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: 1.5rem;
}

.viewer-placeholder {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 400px;
    color: #e2e8f0;
    font-size: 1.1rem;
}

.viewer-container {
    position: relative;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.25);
    background: #fff;
    line-height: 0;
}

.viewer-container.hidden,
.hidden {
    display: none !important;
}

#pdf-canvas {
    display: block;
}

.overlay-layer {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    cursor: crosshair;
}

.overlay-layer.placing {
    cursor: crosshair;
}

.field-overlay {
    position: absolute;
    border: 2px solid;
    background: rgba(255, 255, 255, 0.15);
    cursor: move;
    box-sizing: border-box;
}

.field-overlay.selected {
    box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.5);
    z-index: 10;
}

.field-overlay.type-text {
    border-color: var(--field-text);
}

.field-overlay.type-textbox {
    border-color: var(--field-textbox);
}

.field-overlay.type-checkbox {
    border-color: var(--field-checkbox);
}

.field-overlay.type-radio {
    border-color: var(--field-radio);
}

.field-overlay.type-signature {
    border-color: var(--field-signature);
    border-style: dashed;
}

.field-label {
    position: absolute;
    top: -18px;
    left: 0;
    font-size: 10px;
    line-height: 1;
    padding: 2px 4px;
    background: inherit;
    border: 1px solid;
    border-color: inherit;
    color: var(--text);
    white-space: nowrap;
    pointer-events: none;
    background: rgba(255, 255, 255, 0.9);
}

.resize-handle {
    position: absolute;
    right: -4px;
    bottom: -4px;
    width: 10px;
    height: 10px;
    background: var(--primary);
    border: 1px solid #fff;
    border-radius: 2px;
    cursor: se-resize;
}

.properties-empty {
    color: var(--muted);
    font-size: 0.85rem;
}

.properties-form .form-group {
    margin-bottom: 0.75rem;
}

.properties-form label {
    display: block;
    margin-bottom: 0.25rem;
    font-size: 0.8rem;
    font-weight: 500;
}

.properties-form input[type="text"],
.properties-form input[type="number"],
.properties-form textarea {
    width: 100%;
    padding: 0.4rem 0.5rem;
    font-size: 0.85rem;
    font-family: inherit;
    border: 1px solid var(--border);
    border-radius: 4px;
}

.properties-form input[readonly] {
    background: var(--bg);
    color: var(--muted);
}

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem;
}

.checkbox-label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: normal;
    cursor: pointer;
}

.toast {
    position: fixed;
    bottom: 1.5rem;
    left: 50%;
    transform: translateX(-50%);
    padding: 0.75rem 1.25rem;
    background: #1e293b;
    color: #fff;
    border-radius: 8px;
    font-size: 0.9rem;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
    z-index: 1000;
    max-width: 90vw;
}

.toast.error {
    background: var(--danger);
}

@media (max-width: 900px) {
    .app-main {
        grid-template-columns: 1fr;
        grid-template-rows: auto 1fr auto;
    }

    .toolbar,
    .properties {
        border: none;
        border-bottom: 1px solid var(--border);
    }

    .properties {
        border-bottom: none;
    }
}
