:root {
    --bg: #f4f4f9; --primary: #0056b3; --primary-dark: #004494;
    --surface: #ffffff; --text: #333333; --border: #dddddd;
    --success: #28a745; --danger: #dc3545;
    --secondary: #6c757d;
}
html, body { height: 100%; width: 100%; }
* { box-sizing: border-box; margin: 0; padding: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }
body { background: var(--bg); color: var(--text); overflow-x: hidden; touch-action: manipulation; transition: background-color 0.3s; }

/* App Layout */
.screen { display: none; padding: 20px 15px calc(70px + env(safe-area-inset-bottom)); min-height: 100dvh; }
.screen.active { display: block; }

/* Map View specific layout: Locked absolutely to prevent 100vh mobile overlap issues */
#screen-map-view, #screen-align-map { position: absolute; top: 0; left: 0; right: 0; bottom: calc(50px + env(safe-area-inset-bottom)); display: none; flex-direction: column; overflow: hidden; padding: 0; }
#screen-map-view.active, #screen-align-map.active { display: flex; }

/* Header Anchored to Bottom */
header { position: fixed; top: auto; bottom: 0; left: 0; right: 0; background: var(--primary); color: white; height: calc(50px + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom); display: flex; align-items: center; justify-content: space-between; z-index: 1000; box-shadow: 0 -2px 4px rgba(0,0,0,0.1); }
.header-left, .header-right { width: 50px; height: 50px; flex-shrink: 0; display: flex; align-items: center; justify-content: center; }

/* Breadcrumb Truncation on Left */
#header-title-container { font-size: 0.95rem; flex: 1; margin: 0 5px; direction: rtl; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: normal; height: 50px; line-height: 50px; }
#header-title-container span { direction: ltr; unicode-bidi: isolate; }

/* Square Navigation Buttons */
header .back-btn, header .add-btn { background: none; border: none; color: white; font-size: 1.4rem; width: 50px; height: 50px; line-height: 50px; margin: 0; padding: 0; text-align: center; display: none; cursor: pointer; flex-shrink: 0; }

/* UI Elements */
input[type="text"], input[type="number"], input[type="color"], select, textarea { width: 100%; padding: 12px; margin-bottom: 15px; border: 1px solid var(--border); border-radius: 6px; font-size: 1rem; background: var(--surface); color: var(--text); }
input[type="color"] { height: 50px; padding: 5px; cursor: pointer; }
button { background: var(--primary); color: white; border: none; padding: 12px 20px; border-radius: 6px; font-size: 1rem; width: 100%; margin-bottom: 10px; cursor: pointer; }
button:active { filter: brightness(0.9); }
button.secondary { background: var(--secondary); }
button.danger { background: var(--danger); }
button.success { background: var(--success); }
button.action-btn { width: auto; padding: 8px 15px; margin: 0; font-size: 0.9rem; }

/* Button Groups (Sub-modes) */
.btn-group { display: flex; width: 100%; border: 1px solid var(--primary); border-radius: 6px; overflow: hidden; margin-bottom: 10px; }
.btn-group button { flex: 1; margin: 0; border-radius: 0; background: var(--surface); color: var(--primary); border: none; border-right: 1px solid var(--primary); padding: 8px 5px; font-size: 0.85rem; }
.btn-group button:last-child { border-right: none; }
.btn-group button.active { background: var(--primary); color: white; }

/* Lists & Cards */
.card { background: var(--surface); border-radius: 8px; padding: 15px; box-shadow: 0 1px 3px rgba(0,0,0,0.1); max-width: 640px; margin: 0 auto 15px auto; }
.card h3 { margin-bottom: 5px; }
.card img { max-width: 100%; border-radius: 4px; margin-top: 10px; }
.flex-between { display: flex; justify-content: space-between; align-items: center; }

/* Map Viewer Canvas Area */
#map-container, #align-container { flex: 1; position: relative; width: 100%; background: #e0e0e0; overflow: hidden; order: 1; }
canvas#map-canvas, canvas#align-canvas { display: block; width: 100%; height: 100%; touch-action: none; cursor: grab; }
canvas#map-canvas:active, canvas#align-canvas:active { cursor: grabbing; }

/* Zoom Indicator */
#zoom-indicator { position: absolute; top: 20px; left: 50%; transform: translateX(-50%); background: rgba(0,0,0,0.7); color: white; padding: 5px 15px; border-radius: 20px; font-size: 0.9rem; z-index: 100; opacity: 0; transition: opacity 0.3s; pointer-events: none; }
#zoom-indicator.visible { opacity: 1; }

/* Floating Export Buttons */
.export-container { position: absolute; top: 10px; right: 10px; display: flex; flex-direction: row; gap: 8px; z-index: 100; }
.fab-export { width: auto; background: var(--surface); color: var(--text); border: 1px solid var(--border); padding: 8px 12px; border-radius: 20px; box-shadow: 0 2px 4px rgba(0,0,0,0.3); font-size: 0.85rem; font-weight: bold; cursor: pointer; margin-bottom: 0; }

/* Map Bottom Drawer UI */
#map-drawer { position: absolute; bottom: calc(50px + env(safe-area-inset-bottom)); left: 0; width: 100%; background: var(--surface); border-radius: 16px 16px 0 0; box-shadow: 0 -4px 15px rgba(0,0,0,0.15); z-index: 500; transform: translateY(calc(100% - 40px)); transition: transform 0.3s cubic-bezier(0.25, 0.8, 0.25, 1); display: flex; flex-direction: column; }
#map-drawer.open { transform: translateY(0); }

.drawer-handle { height: 40px; display: flex; flex-direction: column; align-items: center; justify-content: center; cursor: pointer; background: var(--surface); border-radius: 16px 16px 0 0; border-bottom: 1px solid var(--border); }
.drawer-drag-indicator { width: 40px; height: 4px; background: #ccc; border-radius: 2px; margin-bottom: 4px; }
#drawer-status-text { font-size: 0.75rem; font-weight: bold; color: var(--primary); }
.drawer-content { padding: 5px 0 15px; background: var(--surface); touch-action: pan-y; }

.map-hint { font-size: 0.75rem; text-align: center; background: white; padding: 5px; color: #666; flex-shrink: 0; }
.map-toolbar { padding: 10px 15px; background: white; display: flex; gap: 10px; flex-shrink: 0; align-items: center; }
.map-toolbar select { margin-bottom: 0; padding: 10px; font-size: 0.9rem; height: 44px; }

/* Annotation Controls */
.anno-color-btn { width: 24px; height: 24px; border-radius: 50%; cursor: pointer; border: 2px solid transparent; box-shadow: inset 0 0 0 1px rgba(0,0,0,0.1); }
.anno-color-btn.active { border-color: var(--primary); transform: scale(1.15); box-shadow: 0 0 4px rgba(0,0,0,0.3); }
.anno-width-btn { width: 30px; height: 30px; display: flex; align-items: center; justify-content: center; cursor: pointer; border: 2px solid transparent; border-radius: 4px; }
.anno-width-btn.active { border-color: var(--primary); background-color: #eee; }
.anno-width-dot { background: var(--text); border-radius: 50%; }

/* Custom Dropdown */
.custom-select { position: relative; user-select: none; width: 100%; margin-bottom: 15px; height: 44px; }
.custom-select.toolbar-select { margin-bottom: 0; }
.selected-item { padding: 0 10px; height: 100%; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); display: flex; align-items: center; gap: 10px; cursor: pointer; font-size: 0.9rem; }
.dropdown-list { display: none; position: absolute; left: 0; width: 100%; background: var(--surface); border: 1px solid var(--border); max-height: 250px; overflow-y: auto; z-index: 2000; border-radius: 6px; box-shadow: 0 0 15px rgba(0,0,0,0.15); }
.dropdown-item { padding: 12px; display: flex; align-items: center; gap: 10px; cursor: pointer; border-bottom: 1px solid #eee; font-size: 0.9rem; }
.dropdown-item:last-child { border-bottom: none; }
.sprite-icon { width: 24px; height: 24px; display: inline-block; background-size: contain; background-position: center; background-repeat: no-repeat; flex-shrink: 0; }

/* Note History */
.note-card { background: var(--surface); border-radius: 8px; padding: 15px; margin-bottom: 10px; box-shadow: 0 1px 2px rgba(0,0,0,0.1); border-left: 5px solid var(--border); }
.note-header { display: flex; justify-content: space-between; font-size: 0.8rem; margin-bottom: 8px; color: #666; }
.note-author { font-weight: bold; }
.note-status-badge { display: inline-block; padding: 4px 10px; background: #eee; border-radius: 12px; font-size: 0.75rem; margin-bottom: 8px; font-weight: bold; color: #333; text-shadow: 0px 1px 1px rgba(255,255,255,0.5); border: 1px solid rgba(0,0,0,0.1); }
.note-text { white-space: pre-wrap; margin-bottom: 10px; }

/* Modal - Sticky Header Upgrades */
.modal-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); z-index: 2000; display: none; align-items: center; justify-content: center; padding: 15px; }
.modal-content { background: var(--surface); width: 100%; max-width: 400px; border-radius: 10px; padding: 0; box-shadow: 0 5px 15px rgba(0,0,0,0.3); max-height: 90vh; display: flex; flex-direction: column; overflow: hidden; }
.modal-header { padding: 20px 20px 15px 20px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; flex-shrink: 0; background: var(--surface); z-index: 10; margin: 0; }
.modal-header h2 { margin: 0; font-size: 1.3rem; }
.modal-body { padding: 15px 20px 20px 20px; overflow-y: auto; flex: 1; }
.close-modal { background: none; border: none; font-size: 1.5rem; cursor: pointer; color: var(--text); padding: 0; width: auto; margin: 0; }

input[type="file"] { display: none; }