/* ==========================================
   Crazy Little Helper – Styles
   Mobile-first, clean, modern
   ========================================== */

:root {
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --danger: #dc2626;
    --danger-hover: #b91c1c;
    --success: #16a34a;
    --bg: #f8fafc;
    --surface: #ffffff;
    --text: #1e293b;
    --text-muted: #64748b;
    --border: #e2e8f0;
    --radius: 12px;
    --radius-sm: 8px;
    --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.06);
    --shadow-md: 0 4px 6px rgba(0,0,0,0.07), 0 2px 4px rgba(0,0,0,0.06);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
}

/* ---- Navigation ---- */
.navbar {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 0 1rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 56px;
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: var(--shadow);
}

.nav-brand {
    font-weight: 800;
    font-size: 1.25rem;
    color: var(--primary);
    text-decoration: none;
    letter-spacing: -0.5px;
}

.nav-links {
    display: none;
    position: absolute;
    top: 56px;
    left: 0;
    right: 0;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 0.5rem 0;
    box-shadow: var(--shadow-md);
}

.nav-links.open {
    display: block;
}

.nav-links a {
    display: block;
    padding: 0.75rem 1.5rem;
    color: var(--text);
    text-decoration: none;
    font-weight: 500;
}

.nav-links a:hover {
    background: var(--bg);
    color: var(--primary);
}

.nav-logout {
    color: var(--danger) !important;
}

.nav-toggle {
    display: flex;
    flex-direction: column;
    gap: 4px;
    background: none;
    border: none;
    cursor: pointer;
    padding: 8px;
}

.nav-toggle span {
    display: block;
    width: 22px;
    height: 2px;
    background: var(--text);
    border-radius: 1px;
}

/* ---- Container ---- */
.container {
    max-width: 960px;
    margin: 0 auto;
    padding: 1.5rem 1rem;
}

/* ---- Flash Messages ---- */
.flash {
    padding: 0.75rem 1rem;
    border-radius: var(--radius-sm);
    margin-bottom: 1rem;
    font-weight: 500;
}

.flash-error {
    background: #fef2f2;
    color: var(--danger);
    border: 1px solid #fecaca;
}

.flash-success {
    background: #f0fdf4;
    color: var(--success);
    border: 1px solid #bbf7d0;
}

/* ---- Buttons ---- */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    padding: 0.6rem 1.2rem;
    border-radius: var(--radius-sm);
    font-size: 0.875rem;
    font-weight: 600;
    text-decoration: none;
    border: none;
    cursor: pointer;
    transition: all 0.15s;
    white-space: nowrap;
}

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

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

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

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

.btn-danger {
    background: var(--danger);
    color: #fff;
}

.btn-danger:hover {
    background: var(--danger-hover);
}

.btn-ghost {
    background: transparent;
    color: var(--text-muted);
}

.btn-ghost:hover {
    color: var(--text);
    background: var(--bg);
}

.btn-block {
    display: block;
    width: 100%;
    text-align: center;
}

.btn-sm {
    padding: 0.35rem 0.75rem;
    font-size: 0.8rem;
}

.btn-back {
    margin-bottom: 1rem;
}

/* ---- Login ---- */
.login-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 1rem;
}

.login-card {
    background: var(--surface);
    padding: 2rem;
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    width: 100%;
    max-width: 400px;
    text-align: center;
}

.login-card h1 {
    font-size: 1.5rem;
    margin-bottom: 0.25rem;
}

.login-subtitle {
    color: var(--text-muted);
    margin-bottom: 1.5rem;
}

/* ---- Forms ---- */
.form-group {
    margin-bottom: 1rem;
}

.form-group label {
    display: block;
    font-weight: 600;
    font-size: 0.875rem;
    margin-bottom: 0.35rem;
    color: var(--text);
}

.form-group input,
.form-group select,
.form-group textarea {
    width: 100%;
    padding: 0.6rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 1rem;
    font-family: inherit;
    background: var(--surface);
    color: var(--text);
    transition: border-color 0.15s;
}

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

.form-group textarea {
    resize: vertical;
    min-height: 100px;
}

.form-row {
    display: grid;
    grid-template-columns: 1fr;
    gap: 0;
}

.form-section {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem;
    margin-bottom: 1rem;
}

.form-section h2 {
    font-size: 1rem;
    color: var(--text-muted);
    margin-bottom: 1rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--border);
}

.form-section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--border);
}

.form-section-header h2 {
    margin-bottom: 0;
    padding-bottom: 0;
    border-bottom: none;
}

.form-import-bar {
    margin-bottom: 1rem;
}

/* ---- Modal ---- */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
    padding: 1rem;
}

.modal {
    background: var(--surface);
    border-radius: var(--radius);
    width: 100%;
    max-width: 500px;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 25px 50px rgba(0,0,0,0.25);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem;
    border-bottom: 1px solid var(--border);
}

.modal-header h2 {
    font-size: 1.1rem;
    margin: 0;
}

.modal-body {
    padding: 1rem;
    overflow-y: auto;
}

/* ---- HA Device List ---- */
.ha-device-list {
    max-height: 400px;
    overflow-y: auto;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
}

.ha-device-item {
    padding: 0.6rem 0.75rem;
    border-bottom: 1px solid var(--border);
    cursor: pointer;
    display: flex;
    flex-direction: column;
    gap: 0.1rem;
}

.ha-device-item:last-child {
    border-bottom: none;
}

.ha-device-item:hover {
    background: var(--bg);
}

.ha-device-item strong {
    font-size: 0.9rem;
}

.ha-device-item span {
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* ---- AI Status ---- */
.ai-status {
    padding: 0.5rem 0.75rem;
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
    margin-top: 0.75rem;
    background: var(--bg);
    color: var(--text-muted);
}

.ai-status-success {
    background: #f0fdf4;
    color: var(--success);
}

.ai-status-error {
    background: #fef2f2;
    color: var(--danger);
}

.form-actions {
    display: flex;
    gap: 0.75rem;
    align-items: center;
    flex-wrap: wrap;
    margin-top: 1rem;
}

.delete-form {
    margin-left: auto;
}

.inline-form {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1.5rem;
}

.input-inline {
    flex: 1;
    padding: 0.6rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 1rem;
}

.input-inline:focus {
    outline: none;
    border-color: var(--primary);
}

.current-image {
    margin-bottom: 1rem;
}

.current-image img {
    max-width: 200px;
    border-radius: var(--radius-sm);
    margin-bottom: 0.5rem;
    display: block;
}

.remove-image {
    font-size: 0.875rem;
    color: var(--danger);
    cursor: pointer;
}

/* ---- Page Header ---- */
.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
    flex-wrap: wrap;
    gap: 0.75rem;
}

.page-header h1 {
    font-size: 1.5rem;
}

.page-actions {
    display: flex;
    gap: 0.5rem;
}

/* ---- Search ---- */
.search-form {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1.5rem;
}

.input-search {
    flex: 1;
    padding: 0.6rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 1rem;
}

.input-search:focus {
    outline: none;
    border-color: var(--primary);
}

/* ---- Dashboard ---- */
.dashboard-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
    margin-bottom: 2rem;
}

.dashboard-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem;
    text-decoration: none;
    color: var(--text);
    display: flex;
    align-items: center;
    gap: 1rem;
    transition: box-shadow 0.15s, border-color 0.15s;
}

.dashboard-card:hover {
    box-shadow: var(--shadow-md);
    border-color: var(--primary);
}

.dashboard-card-icon {
    font-size: 2rem;
}

.dashboard-card-count {
    display: block;
    font-size: 1.75rem;
    font-weight: 800;
    line-height: 1;
}

.dashboard-card-label {
    font-size: 0.875rem;
    color: var(--text-muted);
}

.quick-actions {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem;
}

.quick-actions h2 {
    font-size: 1rem;
    margin-bottom: 1rem;
    color: var(--text-muted);
}

.quick-actions .btn {
    margin-right: 0.5rem;
    margin-bottom: 0.5rem;
}

/* ---- Device Grid (Listenansicht) ---- */
.device-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 0.75rem;
}

.device-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    text-decoration: none;
    color: var(--text);
    display: flex;
    overflow: hidden;
    transition: box-shadow 0.15s, border-color 0.15s;
}

.device-card:hover {
    box-shadow: var(--shadow-md);
    border-color: var(--primary);
}

.device-card-image {
    width: 80px;
    min-height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    background: var(--bg);
    padding: 6px;
}

.device-card-image img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.device-card-placeholder {
    font-size: 2rem;
}

.device-card-body {
    padding: 0.75rem 1rem;
    display: flex;
    flex-direction: column;
    justify-content: center;
    min-width: 0;
}

.device-card-body h3 {
    font-size: 1rem;
    margin-bottom: 0.25rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.device-card-meta {
    font-size: 0.8rem;
    color: var(--text-muted);
}

/* ---- Badge ---- */
.badge {
    display: inline-block;
    background: #eff6ff;
    color: var(--primary);
    padding: 0.15rem 0.5rem;
    border-radius: 999px;
    font-size: 0.75rem;
    font-weight: 600;
}

.badge-type {
    background: #f0fdf4;
    color: var(--success);
}

.device-card-badges {
    display: flex;
    gap: 0.35rem;
    flex-wrap: wrap;
    margin-bottom: 0.15rem;
}

/* ---- Device View Compact ---- */
.device-view-compact {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
}

.device-view-header {
    display: flex;
    gap: 1rem;
    padding: 1rem;
    border-bottom: 1px solid var(--border);
    align-items: center;
}

.device-view-thumb {
    width: 64px;
    height: 64px;
    border-radius: var(--radius-sm);
    overflow: hidden;
    flex-shrink: 0;
    background: var(--bg);
    padding: 4px;
}

.device-view-thumb img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.device-view-title h1 {
    font-size: 1.2rem;
    margin-bottom: 0.2rem;
    line-height: 1.2;
}

.device-view-badges {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    flex-wrap: wrap;
}

.device-view-sub {
    font-size: 0.8rem;
    color: var(--text-muted);
}

.info-grid {
    overflow: hidden;
}

.info-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem 1rem;
    border-bottom: 1px solid var(--border);
}

.info-item:last-child {
    border-bottom: none;
}

.info-label {
    font-weight: 600;
    font-size: 0.8rem;
    color: var(--text-muted);
    flex-shrink: 0;
    margin-right: 1rem;
}

.info-value {
    text-align: right;
    word-break: break-all;
    font-size: 0.9rem;
}

.info-value code {
    background: var(--bg);
    padding: 0.1rem 0.35rem;
    border-radius: 4px;
    font-size: 0.8rem;
}

.info-value a {
    color: var(--primary);
}

.device-links {
    display: flex;
    gap: 0.5rem;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
    flex-wrap: wrap;
}

.device-description-compact {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
    font-size: 0.9rem;
    line-height: 1.6;
    color: var(--text);
}

.device-ai-compact {
    display: flex;
    gap: 0.4rem;
    padding: 0.75rem 1rem;
    flex-wrap: wrap;
    background: linear-gradient(135deg, #f8faff, #f0fdf8);
}

.device-description {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem;
    margin-bottom: 1rem;
}

.device-description h2 {
    font-size: 1rem;
    color: var(--text-muted);
    margin-bottom: 0.75rem;
}

.description-text {
    line-height: 1.7;
}

/* ---- AI Help ---- */
.device-ai-help {
    background: linear-gradient(135deg, #eff6ff, #f0fdf4);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem;
    margin-top: 1rem;
}

.device-ai-help h2 {
    font-size: 1rem;
    color: var(--text-muted);
    margin-bottom: 0.75rem;
}

.ai-help-buttons {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.btn-ai {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    padding: 0.5rem 1rem;
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
    font-weight: 500;
    text-decoration: none;
    cursor: pointer;
    transition: all 0.15s;
}

.btn-ai:hover {
    border-color: var(--primary);
    color: var(--primary);
    box-shadow: var(--shadow);
}

/* ---- Autocomplete ---- */
.autocomplete-wrap {
    position: relative;
}

.autocomplete-results {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: var(--surface);
    border: 1px solid var(--border);
    border-top: none;
    border-radius: 0 0 var(--radius-sm) var(--radius-sm);
    max-height: 250px;
    overflow-y: auto;
    z-index: 50;
    box-shadow: var(--shadow-md);
}

.autocomplete-item {
    padding: 0.6rem 0.75rem;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    gap: 0.1rem;
    border-bottom: 1px solid var(--border);
}

.autocomplete-item:last-child {
    border-bottom: none;
}

.autocomplete-item:hover {
    background: var(--bg);
}

.autocomplete-item strong {
    font-size: 0.85rem;
    color: var(--text);
}

.autocomplete-item span {
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* ---- List (Rooms etc.) ---- */
.list {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
}

.list-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
}

.list-item:last-child {
    border-bottom: none;
}

.list-item-info {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

/* ---- Empty State ---- */
.empty-state {
    text-align: center;
    padding: 3rem 1rem;
    color: var(--text-muted);
}

.empty-state p {
    margin-bottom: 1rem;
}

/* ---- QR Scanner ---- */
.qr-scanner-container {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    margin-bottom: 1.5rem;
}

.qr-scanner-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
    font-weight: 600;
    font-size: 0.9rem;
}

#qr-reader {
    width: 100%;
}

#qr-reader video {
    border-radius: 0 0 var(--radius) var(--radius);
}

#btn-qr-scan {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.6rem;
}

/* ---- QR Code Page ---- */
.qr-page {
    text-align: center;
    max-width: 400px;
    margin: 0 auto;
}

.qr-page h1 {
    font-size: 1.25rem;
    margin-bottom: 0.25rem;
}

.qr-device-name {
    color: var(--text-muted);
    margin-bottom: 1.5rem;
}

.qr-code-container {
    background: #fff;
    padding: 1.5rem;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    margin-bottom: 1rem;
    display: inline-block;
}

.qr-code-container svg {
    width: 250px;
    height: 250px;
}

.qr-url {
    display: flex;
    gap: 0.5rem;
    align-items: center;
    justify-content: center;
    margin-bottom: 1.5rem;
    flex-wrap: wrap;
}

.qr-url code {
    background: var(--bg);
    padding: 0.4rem 0.75rem;
    border-radius: var(--radius-sm);
    font-size: 0.8rem;
    word-break: break-all;
}

.qr-actions {
    display: flex;
    gap: 0.75rem;
    justify-content: center;
}

/* ---- Print ---- */
@media print {
    .navbar, .page-header, .qr-url, .qr-actions { display: none; }
    .qr-code-container { border: none; padding: 0; }
    .qr-page h1 { font-size: 1rem; }
}

/* ---- Responsive (Tablet+) ---- */
@media (min-width: 640px) {
    .container {
        padding: 2rem 1.5rem;
    }

    .nav-links {
        display: flex;
        position: static;
        background: none;
        border: none;
        padding: 0;
        box-shadow: none;
        gap: 0.25rem;
    }

    .nav-links a {
        padding: 0.5rem 0.75rem;
        border-radius: var(--radius-sm);
    }

    .nav-toggle {
        display: none;
    }

    .form-row {
        grid-template-columns: 1fr 1fr;
        gap: 1rem;
    }

    .device-grid {
        grid-template-columns: 1fr 1fr;
    }

    .device-card-image {
        width: 100px;
        min-height: 100px;
    }
}

@media (min-width: 960px) {
    .device-grid {
        grid-template-columns: 1fr 1fr 1fr;
    }
}
