:root {
    --primary: #0f766e;
    /* Teal 700 */
    --primary-hover: #0d9488;
    /* Teal 600 */
    --primary-light: #f0fdfa;
    /* Teal 50 */
    --secondary: #334155;
    /* Slate 700 */
    --secondary-light: #f1f5f9;
    /* Slate 100 */
    --bg-app: #f8fafc;
    /* Slate 50 */
    --bg-paper: #ffffff;
    --border-color: #e2e8f0;
    /* Slate 200 */
    --text-main: #0f172a;
    /* Slate 900 */
    --text-muted: #64748b;
    /* Slate 500 */
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
    --radius-md: 0.5rem;
    --radius-lg: 0.75rem;
    --font-heading: 'Outfit', sans-serif;
    --font-body: 'Plus Jakarta Sans', sans-serif;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: var(--font-body);
    background-color: var(--bg-app);
    color: var(--text-main);
    height: 100vh;
    overflow: hidden;
}

.app-container {
    display: flex;
    height: 100vh;
}

/* Sidebar */
.sidebar {
    width: 280px;
    background-color: white;
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    z-index: 10;
}

.sidebar-header {
    padding: 1.5rem;
    border-bottom: 1px solid var(--border-color);
}

.logo-area {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    color: var(--primary);
    margin-bottom: 0.25rem;
}

.logo-icon {
    font-size: 1.5rem;
}

.sidebar-header h1 {
    font-family: var(--font-heading);
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--text-main);
}

.subtitle {
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-left: 2.25rem;
    /* Align with text */
}

.nav-menu {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
}

.nav-section {
    margin-bottom: 1.5rem;
}

.nav-section h3 {
    font-size: 0.75rem;
    text-transform: uppercase;
    font-weight: 600;
    color: var(--text-muted);
    letter-spacing: 0.05em;
    margin-bottom: 0.75rem;
    padding-left: 0.75rem;
}

.nav-btn {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    width: 100%;
    padding: 0.75rem 1rem;
    text-align: left;
    background: none;
    border: none;
    border-radius: var(--radius-md);
    color: var(--secondary);
    font-family: var(--font-body);
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
}

.nav-btn i {
    width: 20px;
    text-align: center;
    color: var(--text-muted);
    transition: color 0.2s;
}

.nav-btn:hover {
    background-color: var(--secondary-light);
    color: var(--text-main);
}

.nav-btn:hover i {
    color: var(--primary);
}

.nav-btn.active {
    background-color: var(--primary-light);
    color: var(--primary);
}

.nav-btn.active i {
    color: var(--primary);
}

/* Main Content */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.top-bar {
    height: 70px;
    background-color: white;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 2rem;
    flex-shrink: 0;
}

.top-bar h2 {
    font-family: var(--font-heading);
    font-size: 1.25rem;
    font-weight: 600;
}

.actions {
    display: flex;
    gap: 0.75rem;
}

.action-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 1rem;
    border-radius: var(--radius-md);
    font-family: var(--font-body);
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
}

.action-btn.secondary {
    background-color: white;
    border: 1px solid var(--border-color);
    color: var(--secondary);
}

.action-btn.secondary:hover {
    background-color: var(--secondary-light);
    border-color: var(--text-muted);
}

.action-btn.primary {
    background-color: var(--primary);
    border: 1px solid var(--primary);
    color: white;
}

.action-btn.primary:hover {
    background-color: var(--primary-hover);
    box-shadow: 0 2px 4px rgba(15, 118, 110, 0.3);
}

/* Workspace */
.workspace {
    flex: 1;
    display: flex;
    overflow: hidden;
    gap: 2rem;
    padding: 2rem;
    background-color: var(--bg-app);
}

/* Input Panel */
.input-panel {
    width: 400px;
    background-color: white;
    border-radius: var(--radius-lg);
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-sm);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    overflow: hidden;
}

.panel-header {
    padding: 1rem 1.5rem;
    background-color: var(--secondary-light);
    border-bottom: 1px solid var(--border-color);
}

.panel-header h3 {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--secondary);
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.letter-form {
    padding: 1.5rem;
    overflow-y: auto;
    flex: 1;
}

.form-group-section {
    margin-bottom: 1.5rem;
    padding-bottom: 1.5rem;
    border-bottom: 1px solid var(--border-color);
}

.form-group-section:last-child {
    border-bottom: none;
}

.form-group-section h4 {
    font-size: 0.85rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 1rem;
    font-weight: 700;
}

.form-group {
    margin-bottom: 1rem;
}

.grid-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

label {
    display: block;
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--secondary);
    margin-bottom: 0.4rem;
}

input[type="text"],
input[type="date"],
textarea,
select {
    width: 100%;
    padding: 0.6rem 0.75rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    font-family: var(--font-body);
    font-size: 0.9rem;
    color: var(--text-main);
    transition: border-color 0.2s, box-shadow 0.2s;
}

input:focus,
textarea:focus,
select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-light);
}

textarea {
    resize: vertical;
    min-height: 80px;
}

/* Preview Panel */
.preview-panel {
    flex: 1;
    background-color: var(--secondary-light);
    /* Darker backdrop to make paper pop */
    border-radius: var(--radius-lg);
    border: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.paper-container {
    flex: 1;
    overflow-y: auto;
    padding: 2rem;
    display: flex;
    justify-content: center;
}

.letter-paper {
    background: white;
    width: 210mm;
    /* A4 width */
    min-height: 297mm;
    /* A4 height */
    padding: 20mm;
    /* A4 margins */
    box-shadow: var(--shadow-lg);
    font-family: 'Times New Roman', Times, serif;
    /* Classic letter font or readable sans */
    font-size: 12pt;
    line-height: 1.5;
    color: black;
    position: relative;
}

/* Letter Content Styles to simulate print */
.letter-header {
    margin-bottom: 2rem;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
}

.doctor-info {
    font-size: 10pt;
    line-height: 1.3;
}

.doctor-name {
    font-weight: bold;
    font-size: 14pt;
    margin-bottom: 0.25rem;
    color: #000;
}

.letter-date {
    text-align: right;
    margin-bottom: 3rem;
}

.letter-recipient {
    margin-left: 50%;
    margin-bottom: 3rem;
    font-weight: bold;
}

.letter-subject {
    font-weight: bold;
    margin-bottom: 1.5rem;
    text-decoration: underline;
}

.letter-body {
    text-align: justify;
    margin-bottom: 4rem;
    white-space: pre-wrap;
    /* Respect newlines from textarea */
}

.letter-signature {
    text-align: right;
    margin-top: 2rem;
}

.signature-box {
    height: 100px;
    /* Space for handwritten signature */
}

/* Print Styles */
@media print {
    body * {
        visibility: hidden;
    }

    #printArea,
    #printArea * {
        visibility: visible;
    }

    #printArea {
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        margin: 0;
        padding: 0;
    }

    .letter-paper {
        box-shadow: none;
        width: 100%;
        margin: 0;
        padding: 20mm;
        /* Browser print handles margins, but we enforce inner padding */
    }

    /* Print settings */
    @page {
        size: A4;
        margin: 0;
        /* Remove browser default margins to control valid printable area */
    }
}

.hidden {
    display: none !important;
}

@import url('https://fonts.googleapis.com/css2?family=Great+Vibes&display=swap');

/* Segmented Control */
.segmented-control {
    display: flex;
    background: var(--secondary-light);
    padding: 4px;
    border-radius: var(--radius-md);
    border: 1px solid var(--border-color);
}

.segmented-control input {
    display: none;
}

.segmented-control label {
    flex: 1;
    text-align: center;
    padding: 6px 12px;
    margin: 0;
    font-size: 0.85rem;
    cursor: pointer;
    border-radius: calc(var(--radius-md) - 2px);
    transition: all 0.2s ease;
    color: var(--text-muted);
    font-weight: 500;
}

.segmented-control input:checked+label {
    background: white;
    color: var(--primary);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    font-weight: 600;
}

/* Tabs */
.tabs {
    display: flex;
    gap: 1rem;
    border-bottom: 2px solid var(--secondary-light);
    margin-bottom: 1rem;
}

.tab-btn {
    border: none;
    background: none;
    padding: 0.5rem 0.25rem;
    font-family: var(--font-body);
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--text-muted);
    cursor: pointer;
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    transition: color 0.2s;
}

.tab-btn.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

.tab-content {
    display: none;
    animation: fadeIn 0.3s ease;
}

.tab-content.active {
    display: block;
}

/* File Upload */
.file-upload input {
    display: none;
}

.file-upload label {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 1rem;
    border: 2px dashed var(--border-color);
    border-radius: var(--radius-md);
    color: var(--text-muted);
    font-size: 0.9rem;
    cursor: pointer;
    transition: all 0.2s;
    background: var(--bg-app);
}

.file-upload label:hover {
    border-color: var(--primary);
    color: var(--primary);
    background: var(--primary-light);
}

/* Chips */
.chips-container {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.chip {
    padding: 0.4rem 0.8rem;
    background: var(--secondary-light);
    border: 1px solid var(--border-color);
    border-radius: 20px;
    font-size: 0.8rem;
    color: var(--secondary);
    cursor: pointer;
    transition: all 0.2s;
    user-select: none;
}

.chip:hover {
    background: white;
    border-color: var(--primary);
    color: var(--primary);
    transform: translateY(-1px);
    box-shadow: var(--shadow-sm);
}

.chip:active {
    transform: translateY(0);
}

/* Signature Styling in Preview */
.signature-target {
    font-family: 'Great Vibes', cursive;
    font-size: 2.5rem;
    color: #1e293b;
    line-height: 1.2;
    min-height: 60px;
}

.signature-img-target {
    max-height: 100px;
    max-width: 200px;
    object-fit: contain;
}

/* Mobile Responsiveness */
@media (max-width: 1024px) {
    .app-container {
        flex-direction: column;
        overflow: auto;
    }

    .sidebar {
        width: 100%;
        height: auto;
        border-right: none;
        border-bottom: 1px solid var(--border-color);
    }

    .workspace {
        flex-direction: column;
        overflow: visible;
        padding: 1rem;
    }

    .input-panel {
        width: 100%;
        height: auto;
    }

    .preview-panel {
        height: auto;
        min-height: 500px;
    }

    .letter-paper {
        width: 100%;
        min-height: auto;
        aspect-ratio: 210/297;
    }
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(5px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Dark Mode */
[data-theme="dark"] {
    --bg-app: #0f172a;
    /* Slate 900 */
    --text-main: #f1f5f9;
    /* Slate 100 */
    --text-muted: #94a3b8;
    /* Slate 400 */
    --secondary: #cbd5e1;
    /* Slate 300 */
    --secondary-light: #1e293b;
    /* Slate 800 */
    --border-color: #334155;
    /* Slate 700 */
    /* Keep paper white for contrast/realism */
    --bg-paper: #ffffff;
}

[data-theme="dark"] .sidebar,
[data-theme="dark"] .top-bar,
[data-theme="dark"] .input-panel {
    background-color: #1e293b;
    /* Slate 800 */
    border-color: var(--border-color);
}

[data-theme="dark"] input[type="text"],
[data-theme="dark"] input[type="date"],
[data-theme="dark"] textarea,
[data-theme="dark"] select {
    background-color: #0f172a;
    border-color: #334155;
    color: white;
}

[data-theme="dark"] .file-upload label {
    background-color: #0f172a;
    border-color: #334155;
    color: var(--text-muted);
}

[data-theme="dark"] .nav-btn:hover {
    background-color: #334155;
    color: white;
}

[data-theme="dark"] .segmented-control {
    background: #0f172a;
}

[data-theme="dark"] .segmented-control input:checked+label {
    background: #334155;
    color: white;
}

[data-theme="dark"] .chip {
    background: #334155;
    color: white;
    border-color: #475569;
}

[data-theme="dark"] .chip:hover {
    background: var(--primary);
    border-color: var(--primary);
}

/* Rich Text Toolbar */
.rich-text-toolbar {
    display: flex;
    gap: 0.25rem;
    padding: 0.25rem;
    background: var(--secondary-light);
    border: 1px solid var(--border-color);
    border-bottom: none;
    border-radius: var(--radius-md) var(--radius-md) 0 0;
}

.toolbar-btn {
    padding: 2px 6px;
    background: none;
    border: none;
    cursor: pointer;
    border-radius: 4px;
    color: var(--secondary);
    font-size: 0.85rem;
}

.toolbar-btn:hover {
    background: rgba(0, 0, 0, 0.05);
    color: var(--primary);
}

[data-theme="dark"] .toolbar-btn:hover {
    background: rgba(255, 255, 255, 0.1);
}

.rich-textarea {
    border-top-left-radius: 0 !important;
    border-top-right-radius: 0 !important;
}

/* Prescription Styles (Print) */
.rx-table {
    width: 100%;
    border-collapse: collapse;
    margin: 1rem 0;
}

.rx-table th {
    text-align: left;
    border-bottom: 2px solid #000;
    padding: 0.5rem 0;
    font-size: 10pt;
    text-transform: uppercase;
}

.rx-table td {
    padding: 0.75rem 0;
    border-bottom: 1px dotted #ccc;
    vertical-align: top;
}

.rx-drug {
    font-weight: bold;
    font-size: 11pt;
}

.rx-instruction {
    font-style: italic;
    font-size: 10pt;
    margin-top: 0.2rem;
}

/* Consent Signature (Patient) */
.patient-signature-section {
    margin-top: 3rem;
    border: 1px solid #000;
    padding: 1rem;
    page-break-inside: avoid;
}

.signature-pad-container {
    border: 1px solid var(--border-color);
    background: white;
    /* Always white for canvas */
    border-radius: var(--radius-md);
    position: relative;
    height: 150px;
    margin-bottom: 0.5rem;
}

.signature-pad-container canvas {
    width: 100%;
    height: 100%;
    cursor: crosshair;
}

.clear-sig-btn {
    position: absolute;
    top: 5px;
    right: 5px;
    background: rgba(255, 255, 255, 0.8);
    border: 1px solid #ccc;
    padding: 2px 6px;
    font-size: 0.7rem;
    cursor: pointer;
    border-radius: 3px;
    color: black;
}

/* CCAM Search Widget */
.ccam-widget {
    margin-top: 1rem;
    padding: 1rem;
    background: var(--secondary-light);
    border-radius: var(--radius-md);
    border: 1px solid var(--border-color);
}

.ccam-search {
    width: 100%;
    margin-bottom: 0.5rem;
}

.ccam-results {
    max-height: 150px;
    overflow-y: auto;
    font-size: 0.8rem;
    background: var(--bg-app);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
}

.ccam-item {
    padding: 6px 8px;
    cursor: pointer;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
}

.ccam-item:last-child {
    border-bottom: none;
}

.ccam-item:hover {
    background: var(--primary-light);
    color: var(--primary);
}

[data-theme="dark"] .ccam-item:hover {
    background: #334155;
}