/**
 * Components: Cards, Buttons, Forms, Tables, Search, Alerts, Badges, Tooltips
 * Consolidated from ui.css, ui-cards.css, tooltips.css, and other component files
 * @author    Kaido Mägi
 * @copyright 2025
 */

/* ============================================================
   CARDS
   ============================================================ */

.card {
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 14px;
    padding: 18px;
    box-shadow: 0 1px 6px rgba(0,0,0,.06);
    margin-bottom: 16px;
}

.card-body {
    /* Sisältö wrapper */
}

.card h1,
.card h2 {
    margin-top: 0;
    margin-bottom: 16px;
    font-size: 20px;
    font-weight: 600;
    color: #111827;
}

/* ============================================================
   PANEL (sisäiset paneelit ilman .card-luokkaa)
   ============================================================ */

.panel {
    background: #f9fafb;
    border: 1px solid #e5e7eb;
    border-radius: 10px;
    padding: 16px;
    margin-bottom: 16px;
}

.panel-title {
    font-size: 16px;
    font-weight: 600;
    color: #111827;
    margin: 0 0 12px 0;
}

.customer-contact-info {
    line-height: 1.6;
}

.customer-contact-info p {
    margin: 0 0 8px 0;
}

.customer-contact-info p:last-child {
    margin-bottom: 0;
}

/* Address input wrapper for autocomplete */
.address-input-wrapper {
    position: relative;
    width: 100%;
}

.address-autocomplete-dropdown {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: #fff;
    border: 1px solid #d1d5db;
    border-radius: 8px;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    max-height: 300px;
    overflow-y: auto;
    z-index: 1000;
    margin-top: 4px;
}

.address-autocomplete-dropdown.is-open {
    display: block;
}

.autocomplete-item {
    padding: 10px 12px;
    cursor: pointer;
    border-bottom: 1px solid #f3f4f6;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

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

.autocomplete-item:hover,
.autocomplete-item.selected {
    background-color: #f3f4f6;
}

.autocomplete-item strong {
    color: #111827;
    font-size: 14px;
}

.hidden {
    display: none !important;
}

/* Matched projects list */
.matched-projects-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-top: 16px;
}

.matched-project-item {
    background: #fff;
    border: 1px solid #bfdbfe;
    border-radius: 8px;
    padding: 12px;
}

.matched-project-content {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 12px;
    flex-wrap: wrap;
}

.matched-project-info {
    flex: 1;
    min-width: 0;
}

.matched-project-title {
    display: block;
    margin-bottom: 4px;
}

.matched-projects-note {
    margin-top: 16px;
    margin-bottom: 0;
}

/* ============================================================
   BUTTONS
   ============================================================ */

.btn-primary {
    background: #2563eb;
    color: #fff;
    border-radius: 10px;
    padding: 10px 14px;
    border: 0;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s;
    text-decoration: none;
    display: inline-block;
    font-size: 14px;
}

.btn-primary:hover {
    background: #1d4ed8;
}

.btn-secondary {
    background: #f3f4f6;
    color: #111827;
    border-radius: 10px;
    padding: 10px 14px;
    border: 1px solid #e5e7eb;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s;
    text-decoration: none;
    display: inline-block;
    font-size: 14px;
}

.btn-secondary:hover {
    background: #e5e7eb;
}

.btn-danger {
    background: #dc2626;
    color: #fff;
    border-color: #dc2626;
}

.btn-danger:hover {
    background: #b91c1c;
    border-color: #b91c1c;
}

.btn-success {
    background: #10b981;
    color: #fff;
    border-color: #10b981;
}

.btn-success:hover {
    background: #059669;
    border-color: #059669;
}

.btn-info {
    background: #2563eb;
    color: #fff;
    border-color: #2563eb;
}

.btn-info:hover {
    background: #1d4ed8;
    border-color: #1d4ed8;
}

.btn-ghost {
    background: #fff;
    color: #111827;
    border: 1px solid #e5e7eb;
    border-radius: 10px;
    padding: 10px 14px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s, border-color 0.2s, color 0.2s;
    text-decoration: none;
    display: inline-block;
    font-size: 14px;
}

.btn-ghost:hover {
    background: #f9fafb;
    border-color: #d1d5db;
    color: #111827;
}

.btn-ghost:focus {
    outline: 2px solid #2563eb;
    outline-offset: 2px;
}

.btn-ghost:disabled,
.btn-ghost[disabled] {
    opacity: 0.6;
    cursor: not-allowed;
    color: #6b7280;
    background: #f9fafb;
}

/* ============================================================
   PAGE SELECTOR (lang-selector - sivun valinta, EI kielivalitsin)
   ============================================================ */

.lang-selector {
    display: flex;
    gap: 10px;
    align-items: center;
    margin-bottom: 16px;
}

.lang-selector a {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border-radius: 4px;
    text-decoration: none;
    border: 1px solid #d1d5db;
    color: #374151;
    font-weight: 600;
    font-size: 13px;
    transition: all 0.2s;
}

.lang-selector a:hover {
    background: #f3f4f6;
    border-color: #9ca3af;
}

.lang-selector a.active {
    background: #2563eb;
    color: #ffffff;
    border-color: #2563eb;
}

.btn-icon {
    width: 34px;
    height: 34px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    background: #fff;
    cursor: pointer;
    padding: 0;
    font-size: 18px;
    line-height: 1;
    color: #6b7280;
    transition: background 0.2s, transform 0.1s;
}

.btn-icon:hover {
    background: #f3f4f6;
    color: #111827;
}

.btn-icon:active {
    transform: scale(0.98);
}

.btn-icon:focus {
    outline: 2px solid #2563eb;
    outline-offset: 2px;
}

/* Small button variants */
.btn-primary-small {
    background: #2563eb;
    color: #fff;
    border: none;
    border-radius: 8px;
    padding: 6px 12px;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    text-decoration: none;
    display: inline-block;
}

.btn-secondary-small {
    background: #f3f4f6;
    color: #111827;
    border: 1px solid #d1d5db;
    border-radius: 8px;
    padding: 6px 12px;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    text-decoration: none;
    display: inline-block;
}

/* ============================================================
   BADGES
   ============================================================ */

.badge {
    display: inline-block;
    padding: 4px 10px;
    border-radius: 4px;
    background: #f3f4f6;
    font-size: 12px;
    color: #111827;
    font-weight: 500;
}

.badge-primary {
    background: #dbeafe;
    color: #1e40af;
}

.badge-on {
    background: #d1fae5;
    color: #065f46;
    border: 1px solid #10b981;
    border-radius: 4px;
}

.badge-off {
    background: #fee2e2;
    color: #991b1b;
    border: 1px solid #dc2626;
    border-radius: 4px;
}

/* ============================================================
   FORMS
   ============================================================ */

.form-section {
    margin-top: 28px;
    padding-top: 8px;
}

.form-section:first-child {
    margin-top: 0;
    padding-top: 0;
}

.section-title {
    font-size: 15px;
    font-weight: 700;
    color: #111827;
    margin-bottom: 12px;
    margin-top: 0;
}

.form-section-highlight {
    background: #f9fafb;
    padding: 16px;
    border-radius: 14px;
    border: 1px solid #e5e7eb;
    margin-top: 28px;
}

.form-section-highlight .section-title {
    margin-top: 0;
}

.form-row {
    margin-bottom: 14px;
}

.form-row:last-child {
    margin-bottom: 0;
}

.form-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    margin-top: 12px;
    flex-wrap: wrap;
}

/* Form inputs - basic styles (full styles in utilities or page-specific) */
input[type="text"],
input[type="email"],
input[type="password"],
input[type="date"],
input[type="time"],
input[type="number"],
select,
textarea {
    border-radius: 8px;
    border: 1px solid #d1d5db;
    padding: 8px 12px;
    font-size: 14px;
    font-family: inherit;
    outline: none;
    background: #ffffff;
    transition: border-color 0.12s ease, box-shadow 0.12s ease;
}

input:focus,
select:focus,
textarea:focus {
    border-color: #2563eb;
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

textarea {
    resize: vertical;
}

label {
    display: inline-flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 4px;
    font-weight: 500;
    color: #374151;
    font-size: 14px;
}

/* ============================================================
   SEARCH
   ============================================================ */

.list-toolbar {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 20px;
}

.search-wrap {
    position: relative;
    flex: 1;
    max-width: 520px;
}

.search-input {
    width: 100%;
    padding: 10px 36px 10px 12px;
    border: 1px solid #e5e7eb;
    border-radius: 10px;
    font-size: 14px;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.search-input:focus {
    outline: none;
    border-color: #2563eb;
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.search-input::placeholder {
    color: #9ca3af;
    white-space: normal;
    overflow-wrap: break-word;
}

.search-input {
    min-width: 0;
    box-sizing: border-box;
}

.search-clear {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    border: 0;
    background: transparent;
    cursor: pointer;
    font-size: 20px;
    line-height: 1;
    color: #6b7280;
    padding: 0;
    display: none;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    border-radius: 4px;
    transition: color 0.2s, background 0.2s;
    z-index: 10;
}

.search-input:not(:placeholder-shown) + .search-clear,
.search-input:valid + .search-clear {
    display: flex;
}

.search-clear:hover {
    color: #111827;
    background: #f3f4f6;
}

.search-clear:focus {
    outline: 2px solid #2563eb;
    outline-offset: 2px;
}

.search-meta {
    font-size: 13px;
    color: #6b7280;
    white-space: nowrap;
    min-width: 120px;
}

/* ============================================================
   TABLES
   ============================================================ */

.table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}

.table th,
.table td {
    border-bottom: 1px solid #e5e7eb;
    padding: 8px 6px;
    text-align: left;
    vertical-align: top;
}

.table th {
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: #6b7280;
    background: #f9fafb;
}

.table tr:nth-child(even) td {
    background: #edf0f3;
}

.table-wrapper {
    width: 100%;
    margin-top: 16px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    position: relative;
}

.table-zebra tbody tr:nth-child(even) {
    background: #f9fafb;
}

.table-zebra tbody tr:hover {
    background: #f3f4f6;
}

/* ============================================================
   ALERTS / NOTICES
   ============================================================ */

.notice {
    padding: 12px 16px;
    border-radius: 8px;
    margin-bottom: 16px;
    font-size: 14px;
}

.notice-error {
    background: #fee2e2;
    border: 1px solid #fca5a5;
    color: #991b1b;
}

.notice-success {
    background: #d1fae5;
    border: 1px solid #6ee7b7;
    color: #065f46;
}

.notice-info {
    background: #dbeafe;
    border: 1px solid #93c5fd;
    color: #1e40af;
}

.notice-warning {
    background: #fef3c7;
    border: 2px solid #f59e0b;
    color: #92400e;
}

/* ============================================================
   EMPTY STATE
   ============================================================ */

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    min-height: 160px;
    color: #6b7280;
    gap: 6px;
}

.empty-state .title {
    font-weight: 700;
    color: #111827;
    font-size: 16px;
}

.empty-state > div:not(.title) {
    font-size: 14px;
}

/* ============================================================
   PAGE HEADER
   ============================================================ */

.page-head {
    margin-bottom: 24px;
}

.page-head h1 {
    margin: 0 0 8px 0;
    font-size: 24px;
    font-weight: 700;
    color: #111827;
}

.page-head .subtle {
    font-size: 14px;
    color: #6b7280;
    margin: 0;
}

.page-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 16px;
    margin-bottom: 24px;
    flex-wrap: wrap;
}

.page-header h1 {
    margin: 0;
    font-size: 24px;
    font-weight: 700;
    color: #111827;
    flex: 1;
    min-width: 200px;
}

.page-actions {
    display: flex;
    gap: 10px;
    align-items: center;
    flex-wrap: wrap;
}

/* ============================================================
   PAGE GRID
   ============================================================ */

.page-grid {
    display: grid;
    grid-template-columns: 380px 1fr;
    gap: 24px;
    align-items: start;
    width: 100%;
    margin-top: 24px;
}

.wide-section {
    width: 100%;
    margin-top: 32px;
    padding-top: 24px;
    border-top: 1px solid #e5e7eb;
}

.wide-section .section-title {
    font-size: 18px;
    font-weight: 700;
    color: #111827;
    margin-bottom: 16px;
}

/* ============================================================
   TOOLTIPS
   ============================================================ */

/* Tooltip trigger (ikoninappi) */
.tooltip-trigger {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 18px;
    height: 18px;
    padding: 0;
    margin-left: 6px;
    border: none;
    background: transparent;
    color: #6b7280;
    font-size: 14px;
    line-height: 1;
    cursor: pointer;
    border-radius: 50%;
    transition: all 0.2s ease;
    vertical-align: middle;
    flex-shrink: 0;
}

.tooltip-trigger:hover {
    color: #2563eb;
    background: #eff6ff;
}

.tooltip-trigger:focus {
    outline: 2px solid #2563eb;
    outline-offset: 2px;
}

.tooltip-trigger.tooltip-active {
    color: #2563eb;
    background: #eff6ff;
}

/* Inline tooltip (nappien sisällä) */
.tooltip-trigger.inline-tooltip {
    display: inline;
    margin-left: 6px;
    vertical-align: middle;
    padding: 0;
    border: 0;
    background: transparent;
    font-size: inherit;
    line-height: inherit;
}

/* Vanha .tip-luokka (yhteensopivuus) */
.tip {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 18px;
    height: 18px;
    padding: 0;
    margin-left: 6px;
    border: none;
    background: transparent;
    color: #6b7280;
    font-size: 14px;
    line-height: 1;
    cursor: pointer;
    border-radius: 50%;
    transition: all 0.2s ease;
    vertical-align: middle;
    flex-shrink: 0;
}

.tip:hover {
    color: #2563eb;
    background: #eff6ff;
}

.tip:focus {
    outline: 2px solid #2563eb;
    outline-offset: 2px;
}

.tip.tip-active {
    color: #2563eb;
    background: #eff6ff;
}

/* Tooltip bubble */
.tooltip-bubble {
    position: fixed;
    z-index: 10000;
    max-width: 320px;
    min-width: 200px;
    background: #ffffff;
    border: 1px solid #e5e7eb;
    border-radius: 12px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.12);
    padding: 0;
    opacity: 0;
    transform: translateY(-8px) scale(0.95);
    transition: opacity 0.2s ease, transform 0.2s ease;
    pointer-events: none;
}

.tooltip-bubble.is-open {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: auto;
}

.tooltip-content {
    padding: 12px 14px;
    font-size: 13px;
    line-height: 1.35;
    color: #374151;
    word-wrap: break-word;
}

.tooltip-content p {
    margin: 0;
}

/* Vanha .tooltip-popup (yhteensopivuus) */
.tooltip-popup {
    position: fixed;
    z-index: 10000;
    max-width: 320px;
    min-width: 200px;
    background: #ffffff;
    border: 1px solid #e5e7eb;
    border-radius: 12px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.12);
    padding: 0;
    opacity: 0;
    transform: translateY(-8px) scale(0.95);
    transition: opacity 0.2s ease, transform 0.2s ease;
    pointer-events: none;
}

.tooltip-popup.tooltip-visible {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: auto;
}

/* ============================================================
   PAGE SHELL (yleinen layout-runko kaikille sivuille)
   ============================================================ */

/* Page container: max-width + keskitys, ei "seinästä seinään" */
.page-container {
    width: 100%;
    max-width: 100%;
    margin: 0 24px;
    padding: 24px;
    box-sizing: border-box;
    background: #ffffff;
    border-radius: 14px;
    box-shadow: 0 1px 6px rgba(0, 0, 0, 0.06);
}

@media (max-width: 768px) {
    .page-container {
        padding: 16px;
        margin: 0 16px;
    }
}

/* ============================================================
   PRIMARY TABS (ylätaso: vaalea tab-rivi)
   ============================================================ */

.tabs-primary {
    display: flex;
    gap: 4px;
    margin-bottom: 0;
    padding: 12px 0;
    background: #f9fafb;
    border-bottom: 1px solid #e5e7eb;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
}

.tabs-primary::-webkit-scrollbar {
    height: 6px;
}

.tabs-primary::-webkit-scrollbar-track {
    background: #f3f4f6;
}

.tabs-primary::-webkit-scrollbar-thumb {
    background: #d1d5db;
    border-radius: 3px;
}

.tabs-primary a {
    padding: 10px 16px;
    border-radius: 8px;
    text-decoration: none;
    font-weight: 500;
    font-size: 14px;
    color: #374151;
    background: transparent;
    border: 1px solid transparent;
    transition: all 0.2s;
    white-space: nowrap;
    flex-shrink: 0;
}

.tabs-primary a:hover {
    background: #f3f4f6;
    color: #111827;
}

.tabs-primary a.active {
    background: #2563eb;
    color: #ffffff;
    border-color: #2563eb;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.tabs-primary a.active:hover {
    background: #1d4ed8;
    border-color: #1d4ed8;
}

/* ============================================================
   SECONDARY TABS (toinen taso: sisällön osiot)
   ============================================================ */

.tabs-secondary {
    display: flex;
    gap: 4px;
    margin-bottom: 24px;
    padding: 8px 0;
    border-bottom: 1px solid #e5e7eb;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
}

.tabs-secondary::-webkit-scrollbar {
    height: 6px;
}

.tabs-secondary::-webkit-scrollbar-track {
    background: #f9fafb;
}

.tabs-secondary::-webkit-scrollbar-thumb {
    background: #d1d5db;
    border-radius: 3px;
}

.tabs-secondary a {
    padding: 8px 16px;
    border-radius: 6px;
    text-decoration: none;
    font-weight: 500;
    font-size: 14px;
    color: #6b7280;
    background: transparent;
    border: 1px solid transparent;
    transition: all 0.2s;
    white-space: nowrap;
    flex-shrink: 0;
}

.tabs-secondary a:hover {
    background: #f3f4f6;
    color: #374151;
}

.tabs-secondary a.active {
    background: #eff6ff;
    color: #2563eb;
    border-color: #bfdbfe;
}

.tabs-secondary a.active:hover {
    background: #dbeafe;
    border-color: #93c5fd;
}

/* ============================================================
   PAGE GRID (2-sarakkeinen layout)
   ============================================================ */

.page-grid,
.page-grid-layout {
    display: grid;
    grid-template-columns: 380px 1fr;
    gap: 24px;
    align-items: start;
    width: 100%;
}

@media (max-width: 980px) {
    .page-grid,
    .page-grid-layout {
        grid-template-columns: 1fr;
        gap: 16px;
    }
}

.page-grid aside,
.page-grid-layout .page-grid-left,
.page-grid-layout aside.page-grid-left {
    display: flex;
    flex-direction: column;
    gap: 16px;
    width: 100%; /* Täyttää vasemman sarakkeen */
    min-width: 0; /* Estää overflow */
}

/* Varmista että vasemman sarakkeen kortit käyttävät täyttä leveyttä */
.page-grid aside .card,
.page-grid-layout .page-grid-left .card {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
}

.page-grid section,
.page-grid-layout .page-grid-right,
.page-grid-layout section.page-grid-right {
    display: flex;
    flex-direction: column;
    gap: 16px;
    min-width: 0; /* Estää overflow */
    width: 100%; /* Täyttää oikean sarakkeen */
}

/* Varmista että oikean sarakkeen kortit ja taulukot käyttävät täyttä leveyttä */
.page-grid section .card,
.page-grid section table,
.page-grid-layout .page-grid-right .card,
.page-grid-layout .page-grid-right table {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
}

/* ============================================================
   ADMIN NAVIGATION (legacy, yhteensopivuus)
   ============================================================ */

.admin-nav {
    display: flex;
    gap: 8px;
    margin-bottom: 24px;
    padding-bottom: 16px;
    border-bottom: 2px solid #e5e7eb;
    flex-wrap: wrap;
}

.admin-nav a {
    padding: 8px 16px;
    border-radius: 6px;
    text-decoration: none;
    font-weight: 500;
    transition: all 0.2s;
}

.admin-nav a.active {
    background: #3b82f6;
    color: white;
}

.admin-nav a:not(.active) {
    background: #f3f4f6;
    color: #374151;
}

.admin-nav a:not(.active):hover {
    background: #e5e7eb;
}

/* ============================================================
   RESPONSIVE
   ============================================================ */

@media (max-width: 980px) {
    .page-grid {
        grid-template-columns: 1fr;
    }
    
    .list-toolbar {
        flex-direction: column;
        align-items: stretch;
    }
    
    .search-wrap {
        max-width: none;
        width: 100%;
    }
    
    .search-meta {
        text-align: center;
        min-width: auto;
    }
    
    .page-header {
        flex-direction: column;
    }
    
    .page-actions {
        width: 100%;
    }
}

@media (max-width: 767px) {
    .tooltip-popup {
        max-width: calc(100% - 40px);
        left: 20px !important;
        right: 20px;
        width: auto;
    }

    .tooltip-content {
        padding: 16px 18px;
        font-size: 14px;
    }

    .tooltip-popup.tooltip-mobile-bottom {
        bottom: 20px;
        top: auto !important;
        left: 20px !important;
        right: 20px;
        max-width: none;
        width: calc(100% - 40px);
    }
}

@media (max-width: 480px) {
    .tooltip-popup {
        max-width: calc(100% - 24px);
        left: 12px !important;
        right: 12px;
    }

    .tooltip-content {
        padding: 14px 16px;
        font-size: 13px;
    }
}

