/* Reset and base styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    /* Color palette - Material Design inspired */
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --primary-light: #3b82f6;
    --danger: #dc2626;
    --danger-dark: #b91c1c;
    
    --background: #ffffff;
    --surface: #f8f9fa;
    --surface-elevated: #ffffff;
    --border: #e5e7eb;
    --border-light: #f3f4f6;
    
    --text-primary: #111827;
    --text-secondary: #6b7280;
    --text-tertiary: #9ca3af;
    
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
    --shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
    
    --radius: 0.375rem;
    --radius-lg: 0.5rem;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    color: var(--text-primary);
    background-color: var(--background);
    overflow: hidden;
}

/* App container */
.app-container {
    display: flex;
    height: 100vh;
    width: 100vw;
}

/* Main section */
.main-section {
    flex: 1;
    display: flex;
    flex-direction: column;
    background-color: var(--background);
    min-width: 0;
}

.app-header {
    height: 4rem;
    padding: 0 1.5rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--border);
    background-color: var(--surface-elevated);
}

.app-title {
    font-size: 1.125rem;
    font-weight: 600;
    color: var(--text-primary);
}

/* Canvas container */
.canvas-container {
    flex: 1;
    padding: 1.5rem;
    overflow: hidden;
}

.canvas-wrapper {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.measurement-canvas {
    max-width: 100%;
    max-height: 100%;
    cursor: crosshair;
    box-shadow: var(--shadow-xl);
    border-radius: var(--radius);
}

.measurement-canvas.panning {
    cursor: grabbing;
}

.measurement-canvas.snap-cursor {
    cursor: crosshair;
}

/* Empty state */
.empty-state {
    text-align: center;
    color: var(--text-secondary);
}

.empty-state-title {
    font-size: 1.125rem;
    margin-bottom: 0.5rem;
}

.empty-state-subtitle {
    font-size: 0.875rem;
}

/* Loading state */
.loading-state {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    color: var(--text-secondary);
}

.spinner {
    width: 1.5rem;
    height: 1.5rem;
    border: 2px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Point counter overlay */
.point-counter {
    position: absolute;
    top: 1rem;
    left: 1rem;
    background-color: rgba(255, 255, 255, 0.9);
    backdrop-filter: blur(8px);
    border: 1px solid var(--border);
    padding: 0.375rem 0.75rem;
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    font-size: 0.75rem;
    font-weight: 500;
}

/* Zoom controls */
.zoom-controls {
    position: absolute;
    bottom: 1.5rem;
    right: 1.5rem;
    display: flex;
    gap: 0.5rem;
    background-color: rgba(255, 255, 255, 0.9);
    backdrop-filter: blur(8px);
    border: 1px solid var(--border);
    padding: 0.5rem;
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
}

.zoom-btn {
    background: transparent;
    border: none;
    width: 2.25rem;
    height: 2.25rem;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--text-primary);
    border-radius: var(--radius);
    transition: background-color 0.2s;
}

.zoom-btn:hover {
    background-color: var(--surface);
}

.zoom-btn:active {
    background-color: var(--border-light);
}

.zoom-display {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 4rem;
    padding: 0 0.75rem;
    font-size: 0.875rem;
    font-weight: 500;
}

.zoom-divider {
    width: 1px;
    height: 1.5rem;
    background-color: var(--border);
    margin: 0 0.25rem;
}

/* Tool panel */
.tool-panel {
    width: 22rem;
    max-width: 30vw;
    min-width: 18.75rem;
    background-color: var(--surface);
    border-left: 1px solid var(--border);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 1rem;
    padding: 1.5rem;
}

.panel-section {
    background-color: var(--surface-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 1rem;
}

.panel-section-title {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 0.75rem;
}

/* Upload area */
.upload-area {
    border: 2px dashed var(--border);
    border-radius: var(--radius);
    padding: 2rem 1rem;
    text-align: center;
    cursor: pointer;
    transition: all 0.2s;
    position: relative;
    user-select: none;
    -webkit-user-select: none;
    pointer-events: auto;
    display: block;
    margin: 0;
}

.upload-area:hover {
    border-color: var(--primary);
    background-color: var(--surface);
}

.upload-area:active {
    transform: scale(0.98);
}

.upload-area svg {
    color: var(--text-tertiary);
    margin: 0 auto 0.75rem;
}

.upload-text {
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-primary);
    margin-bottom: 0.25rem;
}

.upload-subtext {
    font-size: 0.75rem;
    color: var(--text-secondary);
}

/* Info card */
.info-card {
    background-color: var(--surface);
    border-radius: var(--radius);
    padding: 0.75rem;
}

.info-text {
    font-size: 0.875rem;
    color: var(--text-secondary);
}

.metric-value {
    font-size: 1.5rem;
    font-weight: 600;
    color: var(--primary);
}

/* Collapsible sections */
.collapsible-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    background: none;
    border: none;
    padding: 0;
    cursor: pointer;
    margin-bottom: 0.75rem;
}

.collapsible-header .panel-section-title {
    margin-bottom: 0;
}

.chevron {
    color: var(--text-secondary);
    transition: transform 0.2s;
}

.collapsible-header[aria-expanded="false"] .chevron {
    transform: rotate(-90deg);
}

.collapsible-content {
    max-height: 1000px;
    overflow: hidden;
    transition: max-height 0.3s ease-out;
}

.collapsible-content.collapsed {
    max-height: 0;
}

/* Polygon list */
.polygon-item {
    background-color: var(--surface);
    border-radius: var(--radius);
    padding: 0.75rem;
    margin-bottom: 0.5rem;
}

.polygon-item:last-child {
    margin-bottom: 0;
}

.polygon-item-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.25rem;
}

.polygon-name-editable {
    position: relative;
    flex: 1;
}

.polygon-name-text {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-primary);
    cursor: pointer;
    display: inline-block;
    padding: 0.125rem 0.25rem;
    border-radius: var(--radius);
    transition: background-color 0.2s;
}

.polygon-name-text:hover {
    background-color: var(--surface);
    color: var(--primary);
}

.polygon-name-input {
    font-size: 0.75rem;
    font-weight: 600;
    font-family: inherit;
    color: var(--text-primary);
    background-color: var(--surface-elevated);
    border: 1px solid var(--primary);
    border-radius: var(--radius);
    padding: 0.125rem 0.25rem;
    width: 100%;
    outline: none;
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.polygon-area {
    font-size: 0.875rem;
    color: var(--primary);
    font-weight: 500;
    margin-left: 0.5rem;
}

.polygon-merge-btn {
    display: flex;
    align-items: center;
    gap: 0.375rem;
    width: 100%;
    padding: 0.5rem;
    margin-top: 0.5rem;
    background-color: var(--surface-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--text-primary);
    cursor: pointer;
    transition: all 0.2s;
}

.polygon-merge-btn:hover {
    background-color: var(--border-light);
    border-color: var(--primary);
    color: var(--primary);
}

.polygon-merge-btn:active {
    background-color: var(--border);
}

.polygon-merge-btn svg {
    flex-shrink: 0;
}

/* Pipes list */
.pipe-item {
    background-color: var(--surface);
    border-radius: var(--radius);
    padding: 0.75rem;
    margin-bottom: 0.5rem;
    border-left: 3px solid var(--primary);
}

.pipe-item:last-child {
    margin-bottom: 0;
}

.pipe-name {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 0.25rem;
}

.pipe-length {
    font-size: 0.875rem;
    color: var(--primary);
    font-weight: 500;
}

.pipes-total {
    background-color: var(--surface);
    border-radius: var(--radius);
    padding: 0.75rem;
    margin-bottom: 0.5rem;
    border: 2px solid var(--primary);
}

.pipes-total-label {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 0.25rem;
}

.pipes-total-value {
    font-size: 1.125rem;
    color: var(--primary);
    font-weight: 600;
}

/* Points list */
.point-item {
    font-size: 0.75rem;
    color: var(--text-secondary);
    padding: 0.5rem 0.75rem;
    background-color: var(--surface);
    border-radius: var(--radius);
    margin-bottom: 0.25rem;
}

.point-item:last-child {
    margin-bottom: 0;
}

/* Instructions */
.instructions-list {
    list-style-position: inside;
    font-size: 0.875rem;
    color: var(--text-secondary);
    line-height: 1.6;
}

.instructions-list li {
    margin-bottom: 0.5rem;
}

.instructions-list li:last-child {
    margin-bottom: 0;
}

/* Tools */
.tools-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.5rem;
}

.tool-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.375rem;
    padding: 0.75rem 0.5rem;
    background-color: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--text-primary);
    cursor: pointer;
    transition: all 0.2s;
}

.tool-btn:hover:not(.active) {
    background-color: var(--border-light);
    border-color: var(--primary);
}

.tool-btn.active {
    background-color: var(--primary);
    color: white;
    border-color: var(--primary);
}

.tool-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    background-color: var(--surface);
}

.tool-btn:disabled:hover {
    background-color: var(--surface);
    border-color: var(--border);
}

.tool-btn svg {
    flex-shrink: 0;
}

.tool-btn span {
    font-size: 0.75rem;
}

/* Controls */
.controls-grid {
    display: grid;
    gap: 0.5rem;
}

.control-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.625rem 1rem;
    background-color: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-primary);
    cursor: pointer;
    transition: all 0.2s;
}

.control-btn:hover:not(:disabled) {
    background-color: var(--border-light);
    border-color: var(--primary);
}

.control-btn:active:not(:disabled) {
    background-color: var(--border);
}

.control-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.control-btn-danger:hover:not(:disabled) {
    border-color: var(--danger);
    color: var(--danger);
}

.control-btn-primary {
    background-color: var(--primary);
    color: white;
    border-color: var(--primary);
}

.control-btn-primary:hover:not(:disabled) {
    background-color: var(--primary-dark);
}

.control-btn.active {
    background-color: var(--primary);
    color: white;
    border-color: var(--primary);
}

.control-btn.active:hover:not(:disabled) {
    background-color: var(--primary-dark);
}

.control-btn svg {
    flex-shrink: 0;
}

/* Modal */
.calibration-modal {
    border: none;
    border-radius: var(--radius-lg);
    padding: 0;
    max-width: 28rem;
    box-shadow: var(--shadow-xl);
}

.calibration-modal::backdrop {
    background-color: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
}

.modal-content {
    padding: 1.5rem;
}

.modal-title {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 0.5rem;
}

.modal-description {
    font-size: 0.875rem;
    color: var(--text-secondary);
    margin-bottom: 1.5rem;
}

.calibration-form {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.form-label {
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-primary);
}

.form-input {
    padding: 0.625rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.875rem;
    font-family: inherit;
    transition: all 0.2s;
}

.form-input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.modal-actions {
    display: flex;
    gap: 0.75rem;
    justify-content: flex-end;
}

.btn {
    padding: 0.625rem 1.25rem;
    border: none;
    border-radius: var(--radius);
    font-size: 0.875rem;
    font-weight: 500;
    font-family: inherit;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-secondary {
    background-color: var(--surface);
    color: var(--text-primary);
    border: 1px solid var(--border);
}

.btn-secondary:hover {
    background-color: var(--border-light);
}

.btn-primary {
    background-color: var(--primary);
    color: white;
}

.btn-primary:hover {
    background-color: var(--primary-dark);
}

.btn-primary:active {
    background-color: #1e40af;
}

/* Toast notification */
.toast {
    position: fixed;
    bottom: 2rem;
    left: 50%;
    transform: translateX(-50%);
    background-color: var(--text-primary);
    color: white;
    padding: 0.875rem 1.25rem;
    border-radius: var(--radius);
    box-shadow: var(--shadow-xl);
    z-index: 1000;
    animation: slideUp 0.3s ease-out;
}

.toast.hidden {
    display: none;
}

@keyframes slideUp {
    from {
        transform: translateX(-50%) translateY(100%);
        opacity: 0;
    }
    to {
        transform: translateX(-50%) translateY(0);
        opacity: 1;
    }
}

.toast-content {
    font-size: 0.875rem;
    font-weight: 500;
}

/* Utility classes */
.hidden {
    display: none !important;
}

/* Responsive design */
@media (max-width: 768px) {
    .tool-panel {
        width: 100%;
        max-width: 100%;
        min-width: 0;
        border-left: none;
        border-top: 1px solid var(--border);
    }
    
    .app-container {
        flex-direction: column;
    }
    
    .main-section {
        height: 60vh;
    }
}
